1

I've been searching and scratching my head for a while on this one. I'm working on a blog page where the user can tag posts, but it is not required. I only want to output the label 'Category' if tags actually exist, so I need a conditional statement in my node template but I can't figure out the correct logic for doing so. This is what I have so far...

<?= if (isset($node->$content['tags'][0]['value'])): ?>
    <p class="label"><?= t("Category"); ?></p>
    <p class="category"><?= render($content['tags']); ?></p>
<?= endif; ?>

This code gives the following PHP error referring to the first line of the block of code

PHP Parse error:  syntax error, unexpected T_IF

I'm a Drupal novice so I'm expecting it to be something simple that I am missing or potentially just completely the wrong code.

2
  • Your server might not have been configured to allow short tags. Try using <?php echo instead of <?=
    – typologist
    Jul 24, 2015 at 17:27
  • I should have said that the short tags are fine, they are not the issue. Jul 24, 2015 at 20:18

1 Answer 1

1

First, you have a syntax error. Your server might not have been configured to allow short tags.

Try using <?php echo instead of <?=

Second, you could either use the Field formatter conditions module to add display conditions to your fields or create a custom code field in Display Suite.

EDIT: How to add a Display Suite custom code field in your own module:

1- Create a new module and add this code in it (remember to change MY_MODULE with the desired name):

/**
 * Implements hook_ds_fields_info().
 *
 * We add the code fields to Display Suite here.
 * Then we specify a callback function that will handle the display logic.
 */
function MY_MODULE_ds_fields_info($entity_type) {
  $fields = array();

  // Add a new custom code field. It should be available in Display Suite.
  $fields['node']['tags_custom'] = array(
    'title' => t('Tags (custom)'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function' => 'MY_MODULE_ds_field_tags_custom',
  );

  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}

/**
 * Callback to show the tags field only if it has a value.
 */
function MY_MODULE_ds_field_tags_custom($field) {
  $node = $field['entity'];

  // Get the tags field value.
  $tags = isset($node->content['tags'][0]['value']) ? $node->content['tags'][0]['value'] : NULL;

  // Return a string or nothing, depending on the tags field value.
  if (!empty($tags)) {    
    return t('Category: ') . $node->content['tags'][0]['value'];
  }
  else {
    return NULL;
  }
}

2- Now, if you go to admin/structure/ds and click on the Manage display link, you will see your new field Tags (custom). Set its label to hidden and publish it in the desired area.

3
  • Ideally I'd want the conditional statement in the template though rather than set through the admin. Jul 24, 2015 at 20:17
  • You can create a custom code field in your code as well. Let me know if you need help to edit my answer accordignly.
    – typologist
    Jul 24, 2015 at 20:34
  • There you go. As you can see, no changes are done in the theme, but in a custom module that will use Display Suite hooks to create a custom field (with your condition) and will make this field available via the admin.
    – typologist
    Jul 24, 2015 at 22:35

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.