0

It is passible to enable or disable the „Display author and date information“ for each node depending on the content of a field?

The base:

  • Drupal 8.3.0 (rc2)

  • field „Where to publish“


  • Case 1: field „Where to publish“ = „public“
  • Case 2: field „Where to publish“ = „internally“

-> in Case 1: disable „Display author and date information“

-> in Case 2: enable „Display author and date information“

How can this be realized? Is that possible? Or should I use two different content types?


First part runs:

function kanu_preprocess_node(&$variables) {
if ($variables['node']->getType() != 'test') {
    return;
  }
$field = $variables['node']->field_where_to_publish;
}

Completed it returns an unexpected error:

function kanu_preprocess_node(&$variables) {
  if ($variables['node']->getType() != 'test') {
      return;
  }
  $field = $variables['node']->field_where_to_publish;

  if (!field->isEmpty() && $field->entity->label() == 'public') {
     $variables['display_submitted'] = FALSE;
  }
}

1 Answer 1

3

You can override the variable display_submitted in a preprocess hook:

mytheme.theme:

function mytheme_preprocess_node(&$variables) {

  // check if this is the right content type where the field exists
  if ($variables['node']->getType() != 'article') {
    return;
  }

  // plain field
  if ($variables['node']->field_where_to_publish->value == 'public') {
    $variables['display_submitted'] = FALSE;
  }

  // taxonomy field
  $field = $variables['node']->field_where_to_publish;
  if (!$field->isEmpty() && $field->entity->label() == 'public') {
    $variables['display_submitted'] = FALSE;
  }

}
14
  • Thank You very much. This looks so simple, but already exceeds my knowledge. Can you please tell me where I have to place this snippet: in my subtheme certainly? In which file? Regards Frank Commented Apr 4, 2017 at 6:07
  • Put the hook in the file mytheme.theme in the root directory of your theme. Replace mytheme both in the file name and the hook name with the name of your theme and clear the cache.
    – 4uk4
    Commented Apr 4, 2017 at 6:38
  • please see above. It doesn't work, but I don't know, why. Commented Apr 5, 2017 at 17:25
  • looks good, only a > before the last { doesn't seem to belong there. Do you get any errors? Do you have a <?php at the beginning of the php file?
    – 4uk4
    Commented Apr 5, 2017 at 17:46
  • ">" - My error when transferring. Is this a problem of the "===" ? Commented Apr 5, 2017 at 18:24

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.