My module adds a link to the node view, from within hook_node_view
. Is there a "Drupal" way to let the site builder control which view modes should show that link?
Right now, I'm using a multiselect control on the module's admin settings page.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIf you are adding fields in hook_node_view then the most polite thing you can do is add them to the 'manage display' tabs for that content type. Do this using hook_field_extra_fields(). This way people can: hide or show them per content type, per view mode and also position them amongst other fields, just like any field API field.
Here is a code example where I have added the print links to the node using hook_node_view().
/**
* Implements hook_node_view().
*/
function zest_general_node_view($node, $view_mode, $langcode) {
// Add print and email links as pseudo field.
$node->content['print_links'] = array(
'#markup'=> print_insert_link() . print_mail_insert_link(),
'#view_mode' => $view_mode,
'#prefix' => '<div class="links inline print-mail">',
'#suffix' => '</div>',
'#weight' => 20,
);
}
Then I add them as a 'pseudo field' using hook_field_extra_fields().
/**
* Implements hook_field_extra_fields().
*
* Adds the print and email links as a pseudo field.
*/
function zest_general_field_extra_fields() {
$extra = array();
$extra['node']['product'] = array(
'display' => array(
'print_links' => array(
'label' => t('Print Links'),
'description' => t('Links provided by the print module.'),
'weight' => 20,
),
),
);
return $extra;
}
$node->content['links']
- can I explain to hook_field_extra_fields()
that my links live inside that array?
Mar 10, 2014 at 19:38
In your theme's template.php, you can invoke hook_preprocess_node() and modify the links array there. Something like the following:
function YOUR_THEME_preprocess_node(&$vars) {
switch ($vars['view_mode']) {
case 'teaser':
// change $vars['content']['links'] here.
break;
}
}
You can invoke that hook from your module, as well. In fact, hook_node_view takes the view_mode as a parameter, so you should be able to set it using that already.
use DisplaySuite and give the ability to allow the site manager to edit the layout for a given view mode (the same thing could be done with allowing the site manager control over Content Types
but this is slightly more dangerous).
You can also try something with the Context module, I believe you can have a context value per node for the view_mode
setting -- but your site architecture would have to support using Context(s).
custom field
or php code
field from the DS admin ui for the content type in question. DS is basically you're all-in-one solution for this. For you the developer, and your site admins: see elevatedthird.com/blog/… the route you're going by hand tooling it is hardest to make friendly for your site administrators.