modes en Drupal 7: How to add a custom node view and force a node template to use it <span>Drupal 7: How to add a custom node view and force a node template to use it</span> <span><span>admin</span></span> <span>Mon, 10/15/2012 - 15:11</span> <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 class="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="" 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="comments" > <a id="comment-1603"></a> <article data-comment-user-id="0" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1350339840"></mark> <footer class="attribution"> <div class="comment-submitted"> <p class="commenter-name"> <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> </div> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/1603#comment-1603" class="permalink" rel="bookmark" hreflang="en">Found this module to be</a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Found this module to be useful : <a href=""></a></p> </div> <div class="sharethis-wrapper"><span st_url="" class="st_facebook"></span> <span st_url="" class="st_email"></span> <span st_url="" class="st_twitter"></span> <span st_url="" class="st_sharethis"></span> <span st_url="" class="st_print"></span> <span st_url="" class="st_fblike"></span> </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> </div> </article> <h2>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-item- js-form-item-"> <label>Language</label> Not specified </div> </div> Mon, 15 Oct 2012 19:11:36 +0000 admin 49 at