How to enable WordLift JSON-LD only for News pages - wordlift

I have main pages in my site there I just will use my handy-dandy-custom schema but I want to use WL in all my blogPosts/articles and then from there I link to this main pages.
How is possible I remove all WordLift inside my existing pages? (where I already mark some entities) and just use for my news one?

You can use the following filter:
add_filter( 'wl_jsonld_enabled', function( $value ) {
// You can perform any check you need, e.g. is this the Home page?
$is_homepage = is_home() || is_front_page();
// or is this a post?
$post_id = is_singular() ? get_the_ID() : null;
// You can also get for the queried object for further checks.
$query_object = get_queried_object();
// Then return true to enable the JSON-LD or false to disable it.
return $value;
} );

Related

Document Controller search handling of non file: URLs

Global documents with a custom URL scheme?
I have a need to cache info via a URL, with a custom scheme - non file:; to allow user access, and otherwise treat such URLs as global so any access via its URL sees the same data. It's just a fancy way to access user defaults.
I'm relying on a document controller's document(url:) to find such URL if its document exits - previously opened.
And yet it doesn't?
Consider this in app's did finish launching:
do {
let ibm = URL.init(string: "https://www.ibm.com")!
let doc = try docController.makeDocument(withContentsOf: ibm, ofType: "myType")
assert((doc == docController.document(for: ibm)), "created document is not found?")
} catch let error {
NSApp.presentError(error)
}
The assert fires!
So I pause and try to figure what I'm doing wrong.
Essentially I'm trying to support non-file: info, in a flat namespace, to provide consistent access and content.
Probably not an answer - why such URL schemes aren't being found but a working solution is to cache anything, front the search method with such a cache, but doing so creates a maintenance issue:
#objc dynamic var docCache = [URL:NSDocument]()
override var documents: [NSDocument] {
let appDocuments = Array(Set([Array(docCache.values),super.documents].reduce([], +)))
return appDocuments
}
override func document(for url: URL) -> NSDocument? {
if let document = super.document(for: url) {
docCache[url] = document
return document
}
else
if let document = docCache[url] {
return document
}
else
{
return nil
}
}
Enjoy.

is_singular() for custom post type in pre_get_posts

I'm trying to modify the query for a specific post type, whatever page (single or archive) is currently displayed. I'm using the pre_get_posts filter with this test:
if ( is_singular( 'evenement' ) || is_post_type_archive( 'evenement' ) ) {
// do something
}
but the is_singular conditional tag never seems to be true when I'm on a single evenement page.
Any idea what I could have missed?
Thanks for helping.
I had the same issue and figured that you can't use is_singular in pre_get_posts because the queried object is not set at this point. It make sense: the query isn't processed yet, and is_singular return true if a post is being displayed - we don't know at this point if the query return any result, as of pre​_get_posts).
You can use instead the post_type query parameter like this:
if($query->get('post_type') === 'evenement') {
// ... do stuff
}
You can use '$query->is_singular' in combination with a post_type check:
if ( $query->is_singular && $query->get('post_type') === 'your_post_type' ) {
// your code
}
If you only test for post type then you will also modify the query for archive pages.

Drupal autocomplete fails to pull out data as a subdomain

I managed to pull out data using autocomplete at my local (http://mysite.dev/swan/autocomplete). The json data is displayed.
But when I applied the same module at live (now a subdomain: http://test.mysite.com/swan/autocomplete with different drupal installs), this autocomplete fails to pull out data. No json data is displayed.
Do you have any idea if this is related to cross domain issue, or any possible cause I might not be aware of?
This is the callback:
/**
* Callback to allow autocomplete of organisation profile text fields.
*/
function swan_autocomplete($string) {
$matches = array();
$result = db_query("SELECT nid, title FROM {node} WHERE status = 1 AND type='organisation' AND title LIKE LOWER ('%s%%')", $string, 0, 40);
while ($obj = db_fetch_object($result)) {
$title = check_plain($obj->title);
//$matches[$obj->nid] = $title;
$matches[$title] = $title;
}
//drupal_json($matches); // fails at safari for first timers
print drupal_to_js($matches);
exit();
}
Any hint would be very much appreciated.
Thanks
It's the conflict with password_policy.module. Other similar modules just do the same blocking. These modules stop any autocomplete query.

How does one get Drupal's current view/page identifier?

What I am looking for is a page_id/view_id that I can use to identify and style specific pages. I would use the title or the url, but there is a chance that it could change if the a higher-up decides that the page should no longer be called Golf, but rather Tee-Time because he likes it better.
Presumably this identifier would not change if the current page were to be a paged view (page 1,2,3,4...).
One way of solving this is the following. It's depending on the url, so if it changes, so does the class-name.
In my themes template.php I implemented hook_preprocess_page:
function mytheme_preprocess_page(&$vars, $hook) {
$body_classes = array();
$body_classes[] = 'page-' . _get_page_name($_SERVER['REQUEST_URI']);
$vars['body_classes'] = implode(' ', $body_classes);
}
function _get_page_name($request_uri) {
static $numeric_subsection = array(
'/node/' => 'node',
);
$preAlias = $request_uri;
$alias = substr(strrchr($preAlias, "/"), 1);
if (strpos($alias, '?') > -1) {
$alias = substr($alias, 0, strpos($alias, '?'));
}
$page_name = $alias;
if (empty($alias)) {
$page_name = 'start';
}
else if (is_numeric($alias)) {
foreach ($numeric_subsection as $section => $pn) {
if (strpos($preAlias, $section) > -1) {
$page_name = $pn;
}
}
}
return $page_name;
}
Then in the main page-template:
<body class="<?php print $body_classes; ?>">
This isn't a generic solution. So you'll probably have to customize this for your specific needs. It will for example need som tweaking to play nicely with path auto.
This depends a little on how your site is put together (panel pages, view pages, "normal" pages). Essentially, you need to figure out what vars are in scope, and then determine which information in them can be used. To determine what is in scope, you can use print_r(array_keys(get_defined_vars())); and then poke around in the individual vars.
An option is to do something in theme_preprocess_page. One option is to get the page data via page_manager_get_current_page(), poke around in there, and then add body classes as needed. Without knowing what you are doing, you essentially need to print_r the results somewhere, look at what you have, and go from there.

Module created page - cached?

This should be a very simple question, but I'm finding it surprisingly hard to find an answer.
I'm creating custom pages using hook_menu that have both static content and dynamic aspects, (mainly from sql queries and views embeds etc...). My question is, how does drupal's cache system treat these custom pages?
I'm concerned because as the traffic ramps up on some occasions, I feel like I need some kind of caching control over the display of these pages, but at this point, I have no idea if they're automatically being cached, or if I need to somehow specify to drupal that I do indeed want them cached.
Just to clarify, these pages are not admin pages, but are accessible by anyone.
The result of a menu callback is cached only when the menu callback returns the output; when the menu callback prints the output, it's not cached.
The code execute to bootstrap Drupal when a page is requested is the following:
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$return = menu_execute_active_handler();
// Menu status constants are integers; page content is a string.
if (is_int($return)) {
switch ($return) {
case MENU_NOT_FOUND:
drupal_not_found();
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
break;
case MENU_SITE_OFFLINE:
drupal_site_offline();
break;
}
}
elseif (isset($return)) {
// Print any value (including an empty string) except NULL or undefined:
print theme('page', $return);
}
drupal_page_footer();
drupal_page_footer() is the function that caches the result.
function drupal_page_footer() {
if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
page_set_cache();
}
module_invoke_all('exit');
}
page_set_cache() is the function that does the real work.
function page_set_cache() {
global $user, $base_root;
if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && page_get_cache(TRUE)) {
// This will fail in some cases, see page_get_cache() for the explanation.
if ($data = ob_get_contents()) {
if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
$data = gzencode($data, 9, FORCE_GZIP);
}
ob_end_flush();
cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers());
}
}
}
The content is compressed (if the zlib is enabled), and saved in the cache.
If you want the cache the output of a custom menu callback, then you just need to return the output, instead of printing it directly.
function mymodule_callback() {
// …
return $output;
}
Instead of caching the output of the page, you can cache the data the module used to build its output. If, in example, the output data is obtained with an SQL query, you can cache the result of the query.

Resources