0

Please I need a hand, I would like to create a new variable like the native "frontpage" variable. I have a content type called "Config", in this CT I have some boolean fields and I need check for this field values to print a view in my page.tpl

Something like this, my example field "display reviews block?", its may be set "yes" or "no". So, I need create a new variable "$display_reviews" according this value and print this in page.tpl:

<?php if (isset($display_reviews)): ?>
 <?php print views_embed_view('reviews', 'block'); ?>
<?php endif; ?>

Sincerly, I dont know how to do this, but i tried something like this:

function mytemplate_preprocess_page(&$variables) {
  if (isset($variables['node'])) {
    $n = $variables['node'];

    if ($variables['node']->type == 'config') {

        if (isset($n->field_display_reviews == 'yes') {
          $variables['display_reviews'] = 'this variable need be created';

        }
    }    

 }
}

UPDATE

Considering I want to print this variable in my node.tpl not in my page.tpl, I have decided to try with directly query, I dont know if is the better and fast way, but this is my code. Its not working yet, this is broking my site, but now I can display the FIELD ID. Im trying to use the field_attach_load to get the field value but no luck:

/**
* Implementation of preprocess_().
*/
function mytemplate_preprocess_node(&$variables, $hook) {

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'config')
  ->propertyCondition('status', 1)
  // value 1 from boolean field
  ->fieldCondition('field_display_reviews','value', '1', '=');
$result = $query->execute();

if (isset($result['node'])) {
  $stories = $result['node'];

  $fields = field_info_instances('node', 'config');

  // Get id of field
  $reviews = $fields['field_display_reviews']['field_id'];
  field_attach_load('node', $stories, FIELD_LOAD_CURRENT, array('field_id' => $reviews));
  $output = field_get_items('node', $stories, 'field_display_reviews');

  $variables['display_reviews'] = $output;



}

} 
2
  • Looks pretty good to me. Just to make sure, is your theme's name mytemplate? If not are you replacing it with the actual theme name?
    – AjitS
    Commented Nov 12, 2018 at 15:15
  • Hi, no no mytemplate is not my theme name, but Im aware of this. I put mytemplate as just an example.
    – NataN
    Commented Nov 12, 2018 at 17:07

1 Answer 1

0

Your code is already there, i think you didn't extract the value of the field_display_reviews correctly, try the following:

 /**
 * Implements hook_preprocess_HOOK().
 */
function mytemplate_preprocess_page(&$variables) {
  if (isset($variables['node'])) {
    $node = $variables['node'];
    if ($node->type == 'config') {
      if (
        isset($node->field_display_reviews[LANGUAGE_NONE][0]['value'])
        && $node->field_display_reviews[LANGUAGE_NONE][0]['value'] == 'yes'
      ) {
        $variables['display_reviews'] = 'this variable need be created';
      }
    }
  }
}

NOTE: if your display_reviews field of type Boolean the value it will be 1 instead of yes, print the value of $node->field_display_reviews[LANGUAGE_NONE][0]['value'] before if to see.

5
  • Hi, thanks for help. I found my error, in the true, I want print the display_reviews variable in my node.tpl not in page.tpl. So preprocess_page is not for this case. I tried do to the same with preprocess_node but its returns the "Notice: Undefined variable: display_reviews" error. Now I got confused becasuse my homepage node is type 'basic', is there where i will print the variable, but the field that i need rescue is from node type 'config'. Can you guide me how can I create this variable?
    – NataN
    Commented Nov 13, 2018 at 17:56
  • Im almost there! Iam using the preprocess_node and field_get_items, but i have not yet able to rescue the field value. what i did until now: function mytemplate_preprocess_node(&$variables, $hook) { $node = $variables['node']; $field_items = field_get_items('node', $node, 'field_display_reviews'); // Get the value from the first item $reviews = $field_items[0]['value']; $variables['display_reviews'] = $reviews; }
    – NataN
    Commented Nov 13, 2018 at 18:33
  • Try with function mytemplate_preprocess_node(&$variables) { $node = $variables['node']; $variables['display_reviews'] = $node->field_display_reviews[LANGUAGE_NONE][0]['value']; }
    – berramou
    Commented Nov 13, 2018 at 21:58
  • For this way, its not working. I have decided to try with direct query.
    – NataN
    Commented Nov 14, 2018 at 14:10
  • Why you need a direct query to get field_display_reviews ??
    – berramou
    Commented Nov 14, 2018 at 22:06

Your Answer

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

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