How can I prevent article titles from becoming links in Drupal 8?

by mathias.schinner , in category: PHP , a year ago

How can I prevent article titles from becoming links in Drupal 8?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lynn , a year ago

@mathias.schinner 

In Drupal 8, article titles are automatically linked to the article node by default. However, you can prevent this behavior by using a custom theme or a module to override the default settings. Here are two methods to prevent article titles from becoming links in Drupal 8:


Method 1: Using a custom theme

  1. Create a custom theme or use an existing one.
  2. Create a new file called node--article.html.twig in your custom theme's templates directory.
  3. Add the following code to the node--article.html.twig file:{% extends 'node.html.twig' %} {% block title %} {{ node.label }} {% endblock %}
  4. Save the file and clear the Drupal cache.


This method will override the default node.html.twig template and remove the link from the article title.


Method 2: Using a module

  1. Create a new module or use an existing one.
  2. Create a new file called mymodule.module in your module's directory.
  3. Add the following code to the mymodule.module file:/** * Implements hook_entity_view_alter(). */ function mymodule_entity_view_alter(array &$build, DrupalCoreEntityEntityInterface $entity, DrupalCoreEntityDisplayEntityViewDisplayInterface $display) { if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'article') { $build['title'][0]['#markup'] = $entity->getTitle(); } }
  4. Save the file and enable the module.
  5. Clear the Drupal cache.


This method will use the hook_entity_view_alter() hook to modify the article node's title and remove the link.


Either of these methods should prevent article titles from becoming links in Drupal 8.

by diana_barrows , 4 months ago

@mathias.schinner 

Note: Always backup your site before modifying any theme or writing custom code.


Method 1: Using a custom theme

  1. Create a custom theme or use an existing one.
  2. Create a new file called node--article.html.twig in your custom theme's templates directory.
  3. Add the following code to the node--article.html.twig file: {% extends 'node.html.twig' %} {% block title %} {{ node.label }} {% endblock %} This code extends the node.html.twig template and overrides the title block to display just the title without the link.
  4. Save the file and clear the Drupal cache. By clearing the cache, Drupal will recognize the new template file.


This method will override the default node.html.twig template and remove the link from the article title.


Method 2: Using a module

  1. Create a new module or use an existing one.
  2. Create a new file called mymodule.module in your module's directory.
  3. Add the following code to the mymodule.module file: getEntityTypeId() == 'node' && $entity->bundle() == 'article') { $build['title'][0]['#markup'] = $entity->getTitle(); } } This code uses the hook_entity_view_alter() hook to modify the article node's title and remove the link.
  4. Save the file and enable the module. Enable the module in the Drupal administration interface or by running the appropriate Drush command.
  5. Clear the Drupal cache. By clearing the cache, Drupal will recognize the new module and its implementation of the hook.


This method will modify the article node's title when it is rendered, effectively removing the link.


Either of these methods should prevent article titles from becoming links in Drupal 8. Choose the method that suits your needs and technical expertise.