modes https://shivanjaikaran.com/ en Drupal 7: How to add a custom node view and force a node template to use it https://shivanjaikaran.com/drupal-7-how-add-custom-node-view-and-force-node-template-use-it <span class="field field--name-title field--type-string field--label-hidden">Drupal 7: How to add a custom node view and force a node template to use it</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span>admin</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 10/15/2012 - 15:11</span> <div class="node__links"> <ul class="links inline"><li class="comment-add"><a href="/drupal-7-how-add-custom-node-view-and-force-node-template-use-it#comment-form" title="Share your thoughts and opinions." hreflang="und">Add new comment</a></li></ul> </div> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>This is a small Drupal 7 tip if you wanted to create a custom node view and force a node template to use this custom node view.</p> <p><strong>Use case</strong></p> <p>Let's say you are creating a custom node template for a node of type "grades". You may need to show/hide specific fields in your content type for your Grades template. (This is just an example but you can use this tip in various ways).</p> <p><strong>The Steps</strong></p> <ol> <li>You can create a new view mode by putting this code into your custom module:</li> <li> <code><br /> /** * Implements hook_entity_info_alter(). */<br /> function MYMODULE_entity_info_alter(&amp;$entity_info) { $entity_info['node']['view modes']['custom_teaser'] = array( 'label' =&gt; t('Your Custom Teaser'), 'custom settings' =&gt; TRUE, ); } </code></li> <li>Then go to Manage Displays in your content type and set which fields you want to show/hide in the "Your Custom Teaser" view mode.</li> <li>Now you have to force the viewing of your node to use this custom node view "Your Cusom Teaser". Put this code into your custom module: <code> function MYMODULE_preprocess_node(&amp;$variables) { if ($variables['elements']['#view_mode'] == 'custom_teaser') { $variables['theme_hook_suggestions'][] = "node__grades"; } } </code><br /> This will force the viewing of your custom content type "grades" to use grade.tpl.php</li> </ol> <p><strong>Further Reading</strong> Here is how you would make a <a href="http://mearra.com/blogs/juha-niemi/drupal-7-custom-node-view-modes" rel="nofollow">View use your custom node view for Drupal 7</a>.</p> </div> <div class="field field--name-taxonomy-vocabulary-2 field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tag/drupal" hreflang="en">Drupal</a></div> <div class="field__item"><a href="/tag/drupal-7" hreflang="en">Drupal 7</a></div> <div class="field__item"><a href="/tag/tip" hreflang="en">tip</a></div> <div class="field__item"><a href="/tag/custom" hreflang="en">custom</a></div> <div class="field__item"><a href="/tag/node" hreflang="en">node</a></div> <div class="field__item"><a href="/tag/view" hreflang="en">view</a></div> <div class="field__item"><a href="/tag/modes" hreflang="en">modes</a></div> <div class="field__item"><a href="/tag/template" hreflang="en">template</a></div> <div class="field__item"><a href="/tag/drupal-planet" hreflang="en">Drupal Planet</a></div> </div> </div> <section class="field field--name-comment-node-page field--type-comment field--label-hidden comment-wrapper"> <article role="article" data-comment-user-id="0" id="comment-1603" class="comment js-comment by-anonymous clearfix"> <span class="hidden" data-comment-timestamp="1350339840"></span> <footer class="comment__meta"> <p class="comment__author"><span>Anonymous (not verified)</span></p> <p class="comment__time">Mon, 10/15/2012 - 18:24</p> <p class="comment__permalink"><a href="/comment/1603#comment-1603" hreflang="en">Permalink</a></p> </footer> <div class="comment__content"> <h3><a href="/comment/1603#comment-1603" class="permalink" rel="bookmark" hreflang="en">Found this module to be</a></h3> <div class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Found this module to be useful : <a href="http://drupal.org/project/view_mode_templates">http://drupal.org/project/view_mode_templates</a></p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=1603&amp;1=default&amp;2=en&amp;3=" token="OOUWIC9pKVbIpeRj6cOVEKEnbR2N_VUX8NUiJcTYdQA"></drupal-render-placeholder> </div> </article> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=49&amp;2=comment_node_page&amp;3=comment_node_page" token="7k3Z2bSnx3W_9A9KojC5vjZrabC_7lLGPxtiAQeUmxo"></drupal-render-placeholder> </section> <div id="field-language-display"><div class="js-form-item form-item js-form-type-item form-type-item js-form-item- form-item-"> <label>Language</label> Not specified </div> </div> Mon, 15 Oct 2012 19:11:36 +0000 admin 49 at https://shivanjaikaran.com