0

I want articles to show Submitted on without showing Submitted by. The checkbox on Display settings toggles both, not only one of them.
Control/modify display settings (author and date) on posts explains how to do it in Drupal 7.

How should I achieve it in Drupal 8, for example with the Bartik theme?

  • You probably should create a sub-theme of Bartik first to simply edit the templates. You also could create a pseudo field from a custom module. – leymannx Apr 17 at 9:24
  • do you have a custom module or theme in your project? – Yuseferi Apr 17 at 14:12
1

It's enough to edit the node.html.twig template used from the theme. For example, the Bartik theme uses a node.html.twig template containing the following code. (I omitted the documentation comment at the beginning of the file.)

{%
  set classes = [
    'node',
    'node--type-' ~ node.bundle|clean_class,
    node.isPromoted() ? 'node--promoted',
    node.isSticky() ? 'node--sticky',
    not node.isPublished() ? 'node--unpublished',
    view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
    'clearfix',
  ]
%}
{{ attach_library('bartik/classy.node') }}
<article{{ attributes.addClass(classes) }}>
  <header>
    {{ title_prefix }}
      {% if label and not page %}
      <h2{{ title_attributes.addClass('node__title') }}>
        <a href="{{ url }}" rel="bookmark">{{ label }}</a>
      </h2>
    {% endif %}
    {{ title_suffix }}
    {% if display_submitted %}
      <div class="node__meta">
        {{ author_picture }}
        <span{{ author_attributes }}>
          {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
        </span>
        {{ metadata }}
      </div>
    {% endif %}
  </header>
  <div{{ content_attributes.addClass('node__content', 'clearfix') }}>
    {{ content }}
  </div>
</article>

The line printing the name of the user who created the node and when it was posted is the following one.

{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}

It's enough to change it and remove the part printing the username. In the case of the Bartik theme, the node.html.twig content will become the following one.

{%
  set classes = [
    'node',
    'node--type-' ~ node.bundle|clean_class,
    node.isPromoted() ? 'node--promoted',
    node.isSticky() ? 'node--sticky',
    not node.isPublished() ? 'node--unpublished',
    view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
    'clearfix',
  ]
%}
{{ attach_library('bartik/classy.node') }}
<article{{ attributes.addClass(classes) }}>
  <header>
    {{ title_prefix }}
      {% if label and not page %}
      <h2{{ title_attributes.addClass('node__title') }}>
        <a href="{{ url }}" rel="bookmark">{{ label }}</a>
      </h2>
    {% endif %}
    {{ title_suffix }}
    {% if display_submitted %}
      <div class="node__meta">
        {{ author_picture }}
        <span{{ author_attributes }}>
          {% trans %}Submitted on {{ date }}{% endtrans %}
        </span>
        {{ metadata }}
      </div>
    {% endif %}
  </header>
  <div{{ content_attributes.addClass('node__content', 'clearfix') }}>
    {{ content }}
  </div>
</article>

For a different theme, it's enough searching the line of the node.html.twig template containing {{ author_name }} and remove it. Clearly, also the sentence containing {{ author_name }} needs to be changed to make it grammatical. (Submitted by on {{ date }} would not be grammatically correct.)

As general suggestion, never change a template file used from a Drupal core theme or a third-party theme. Rather, create a sub-theme using that theme as parent theme, and add the template files you need to change.

| improve this answer | |

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.