简体   繁体   中英

Drupal 7 - Detect if render($page['content']) contains specific content type

Noob junior here so apologies if this is basic and can't be done. I would, in the interest of developing my knowledge love feedback :)

Lets say I have a basic page:

1   <section>
2       <div class="container">
3           <?php print render($page['content']); ?>
4       </div>
5   </section>

In the rendered content there is a content type called 'fifty-fifty' which outputs:

<div class="fifty_fifty_banner">
    <div class="half">
        left
    </div>
    <div class="half">
        right
    </div>
</div>

What I need is for pages with that "fifty fifty" banner in to have a full width container and for other pages to have 960px width container.

In the first block of code, I would like line 2 to contain an if statement or some check that adds a class if it does, possible?

Edit 1:

Tried if(array_key_exists()) AND if(in_array()) on two pages, one with and one without the fifty fifty banner.

array_key_exists() returns false on both pages.

if(in_array()) returns true on both pages

I guess you could do something like this in your page.tpl.php file, to add a class for the Flexible Page Survey content type:

if ($variables['node']->type == 'content_type_machine_name' && isset($variables['node']->field_paragraph_name)) {
  ?>
  <div class="add-your-class-here">

  </div>
  <?php

}
?>

/admin/structure/types/manage/content_type_machine_name/fields

This will be your field_paragraph_name

在此处输入图片说明

Don't do any of the conditional work in the template itself.

Do the business logic in hook_preprocess_page() of template.php and simply modify the classes of the render block/html if the content type has the format you want in the soon-to-be-render render arrays.

Easiest way to do this is install the Context module . Steps as follows:

1.) Create a new context and set the node type to your content type under the Conditions 在此处输入图片说明 2.) Create a reaction and set it to theme html and give your body a class 在此处输入图片说明 3.) Style your css based off of said class off the body tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM