2

In my Drupal 8 website I created a custom view mode through the UI. I use it to output nodes using a view.

In the view result I do not want the titles of the nodes to appear. The UI (content type > manage display) does not seem to give the option to hide the title.

How can I hide the title in a specific view mode for an entity via the UI or via custom code?

  • Given that the title field is a "normal" field now in D8 (compared to it being a "special" field back in de D7-days) I would expect to be able to hide it in the entity's manage display page. – batigolix Oct 23 '15 at 7:18
  • Apparently one has to install the title module to achieve this - drupal.org/node/2353867 – batigolix Sep 16 '16 at 20:41
1

The output of the title is different from the other fields. The reasons for that is, that the title is used for the title block and as metadata in the html head:

html.html.twig:
<!DOCTYPE html>
<html{{ html_attributes }}>
  <head>
    <head-placeholder token="{{ placeholder_token|raw }}">
    <title>{{ head_title|safe_join(' | ') }}</title>

If the node is used in another context, for example when it is part of a view, the title is placed in the node template as {{ label }}:

node.html.twig:
  {% if not page %}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
  {% endif %}

This is controlled by the variable page, a boolean, which is true, if the view mode is full. This is standard in drupal, when the node is displayed as main content and the title should appear in the head.

If you want to change this default behavior, you have to edit the node twig or change the variable page in preprocess.

In preprocess this would look like this:

function theme_preprocess(&$variables, $hook) {

   if ($hook == 'node') {
      if ( $variables['elements']['#view_mode'] == 'teaser' ) {
         $variables['page'] = true;
      }
   }
}

This will remove the title from the view mode teaser.

Edit:

To answer the specific question in the latest comment, how to make the title field manageable in the display UI:

You have to change the field definition in a hook:

function mymodule_entity_base_field_info_alter(&$fields, $entity_type) {
  if ($entity_type->id() == 'node') {
    if (isset($fields['title'])) {
      $fields['title']->setDisplayConfigurable('view', TRUE);
    }
  }
}

Don't forget to remove {{ label }} from node twig, so that the title is not displayed twice.

  • Yes, I want to know this bit: "change the variables page/label/title in preprocess." I dont want to change to fields in the view because I want a standard approach to content layout in the whole site – batigolix Oct 23 '15 at 7:16
  • The standard approach would be to change the twig template, for example if you want to change the title display in the view of the taxonomy term (which is included in the standard install), you put a twig with the name "node--view--taxonomy-term.html.twig" in your template and remove there the title (named here as label), but if you prefer this in code, use the hook as mentioned in the answer and change 'teaser' to your custom view mode. – 4k4 Oct 23 '15 at 11:14
  • I would like to know how to achieve this using the manage display UI – batigolix Sep 16 '16 at 20:42
  • As mentioned in the answer, in a custom view mode this is not an option in ui. You need to make the base field title configurable first (with a custom module). The probem is still, that the title is hard coded in the twig. You would have to deal with this like mentioned in the answer. A better solution might be, to switch the view from displaying content to fields. – 4k4 Sep 16 '16 at 20:52
  • I've added a code example how to change the field definition (and edited the answer to the original question, there was some work to do to make it comprehensible) – 4k4 Sep 17 '16 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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