Skip to content

Instantly share code, notes, and snippets.

@walidvb
Created May 24, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walidvb/e14aaabb194e01d11c9774dcad27f573 to your computer and use it in GitHub Desktop.
Save walidvb/e14aaabb194e01d11c9774dcad27f573 to your computer and use it in GitHub Desktop.
Fetching a node context from url alias
<?php
function my_module_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
// in plugins/arguments/
<?php
$plugin = array(
'title' => t("Concert: Title"),
'description' => t('Creates a node context from a concert title argument.'),
'context' => 'gc_custom_argument_node_title_context',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the node title of a node for this argument'),
),
);
function gc_custom_argument_node_title_context($arg = NULL, $conf = NULL, $empty = FALSE) {
// watchdog('ctools',
// 'I am running. arg: ' . $arg,
// WATCHDOG_NOTICE); // uncomment and watch wd for debug
if ($empty) {
return ctools_context_create_empty('node');
} // not sure ...
if (is_object($arg)) {
return ctools_context_create('node', $arg);
} // ...if these two are really necessary. Do we ever get an object?
// pain, as this requires to have the full node alias, rather than just the node title "hyphenated"
$alias = "concert/$arg";
$path = drupal_lookup_path("source", $alias);
$node = menu_get_object("node", 1, $path);
if (!$node) {
return FALSE;
}
return ctools_context_create('node', $node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment