1

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.

3 Answers 3

1

If 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;
}

hook_field_extra_fields()

3
  • Thanks! I've thought of that, but the standard way to add links, if I'm not mistaken, is to add them to the existing array $node->content['links'] - can I explain to hook_field_extra_fields() that my links live inside that array?
    – infojunkie
    Mar 10, 2014 at 19:38
  • Sorry I don't know that off the top of my head. Funnily enough, whilst that might be the "correct" place to put it, it diminishes flexibility. It was for this exact reason that I wrote the code I just shared, I needed more control over placement of the print links, so I had to implement this instead of using default functionality. If only print module had provided pseudo fields rather than saying they needed to be placed in some arbitrary 'links' container of which I had no placement control.
    – leexonline
    Mar 10, 2014 at 20:21
  • I'll try this approach and feedback here.
    – infojunkie
    Mar 10, 2014 at 20:28
0

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.

4
  • Thanks, but that's not what I'm looking for. As per my original question, I want to let the site builder select the view modes where the link should appear.
    – infojunkie
    Mar 10, 2014 at 18:14
  • OK, the phrase "site builder" wasn't clear. Set up an admin page using hook_menu, and, in it, display a simple form, say, with a select dropdown of display modes. Then, you can store the administrator's choices in the variables table, using variable_set. you can then use the logic above, while adding a variabl_get call to fetch the stored settings.
    – Ollie
    Mar 10, 2014 at 19:04
  • Yes, that's what I am doing right now. But my question is whether there's a more "standard" way to do the same.
    – infojunkie
    Mar 10, 2014 at 19:39
  • I think you've been given a couple of standard ways to do this, including @tenken's :) Take your pick.
    – Ollie
    Mar 10, 2014 at 20:35
0

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).

3
  • I was hoping to avoid other 3rd party modules, like DS or Context. Besides, I would need to let these modules know about my link.
    – infojunkie
    Mar 10, 2014 at 18:38
  • In DS you could make a 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.
    – tenken
    Mar 10, 2014 at 19:45
  • I respectfully disagree. DS is definitely well-intentioned and comprehensive, but I can't add it as a dependency to a tiny module that just wants to let a site builder control where a link should appear. At most, I could implement a DS-specific hook that will give DS users better control over the placement of my content - and then I should do the same for Panels, Context, etc.
    – infojunkie
    Mar 10, 2014 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.