2

In Drupal 7 the Teaser view mode automatically comes with a 'read more' theme link. I need to retain the Teaser view mode and then create another called 'Snippet', which is basically the same as Teaser but has a lot less content in the trimmed area.

I created a custom module and added the view mode...

/**
* Implements hook_entity_info_alter().
*/
function mymodule_entity_info_alter(&$entity_info) {
    // Add a custom view mode
    $entity_info['node']['view modes']['snippet'] = array(
        'label' => t('Snippet'),
        'custom settings' => TRUE,
    );
}

But it doesn't activate the 'read more' link like Teaser does. Can anyone point me in the right direction with this please?

0

You can use Display Suite for this. Each view mode gives you a screen like below with control over fields and pseudo-fields.

screenshot

  • would welcome feedback on the downvote – Andy Feb 25 '14 at 15:15
  • 1
    I didn't downvote you, but my thought is that if you can add new view modes without using DS, you should be able to configure the core-provided links as well. – akalata Jun 24 '14 at 20:53
  • @akalata You certainly can by doing it in code, which is more lightweight and flexible. Using DS you can create the view mode and set up the link all via the UI and export to code with Features. You require the DS module, but reduce the complexity in both setting up and maintaining the view mode. Both are valid approaches imho. – Andy Jun 25 '14 at 9:53
6

I took a look here

https://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_build_content/7

and here

https://drupal.org/node/1844292

and came up with this

function YOURMODULE_node_view_alter(&$build) {
    if ($build['#view_mode'] == 'YOURVIEWMODE') {
        $node=$build['#node'];
        $links=array();     
        // Read more for teas ext
        $node_title_stripped = strip_tags($node->title);
        $links['node-readmore'] = array(
            'title' => t('Read more<span class="element-invisible"> about @title</span>', array('@title' => $node_title_stripped)),
            'href' => 'node/' . $node->nid,
            'html' => TRUE,
            'attributes' => array('rel' => 'tag', 'title' => $node_title_stripped)
        );
        // Assign links
        $build['links']['node']['#links']=$links;
    }
}
2

Add a new field 'Content:link' then give it a label none and 'Read more' in text to display.

  • It is useful if "Fields" are used in case of the Format is "Unformatted list", and Show is "Fields". – DanielTuzes Aug 30 '16 at 19:53
0

you may add content and theming options in your

 mymodule_node_view($node,$view_mode,$langcode){}

you may customize your content through $node->content.

if you created a custom node you may use

mynodemodule_view($node,$view_mode,$langcode);

instead of hook_node_view

even you may attach a new tpl file or theming function for your node by $node->content['#theme'] = 'my_theme_item';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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