Hot answers tagged

27 votes
Accepted

How to override page title by content type

As mentioned by Ivan Jaros You can use hook_preprocess_page_title. You just have to load the node from the route first, to get some context. function yourtheme_preprocess_page_title(&$variables) ...
  • 1,237
21 votes
Accepted

How to use presave hook to save a field value as node title?

You need to implement the hook_entity_presave() /** * Implements hook_entity_presave(). */ function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { switch ($entity->...
20 votes
Accepted

What's hook_node_view() alternative?

hook_node_view() hasn't really been removed, the way it's invoked has just changed. As you mentioned, it's now handled by hook_ENTITY_TYPE_view(). So you can either use: hook_entity_view() or ...
  • 164k
17 votes
Accepted

Get display name from hook_preprocess_views_view

You can get the display ID through the current_display property of the View object, e.g. $view = $vars['view']; if ($view->name == 'foo' && $view->current_display == 'bar') { // ... }
  • 164k
17 votes

How do I check the value of a field if its empty?

@vinhd proposition is correct, by the way you can also use the isEmpty() method from the field list interface. It can be more adapted for complex field types. if($node->hasField('field_your_field') ...
16 votes
Accepted

Check content type of node in preprocess hook

To get the content type of a node, you just use $variables['node']->getType() or $variables['node']->bundle(). The first method just calls the second, so calling one or the other one doesn't ...
  • 95k
15 votes
Accepted

Twig template suggestions for user default, compact, and user account view modes

You can do this by creating a theme suggestion alter hook in your theme. /** * Implements hook_theme_suggestions_user_alter(). * * An array of alternate, more specific names for template files ...
14 votes
Accepted

How to programmatically truncate table in Drupal8

Sure, the function still exists: https://api.drupal.org/api/drupal/core%21includes%21database.inc/function/db_truncate/8.5.x. It's deprecated, though, with the advice: Instead, get a database ...
  • 164k
13 votes

Is there a way to hook on cache clearing?

The way to do this is to use hook_flush_caches in combination with register_shutdown_function. Example code: /** * Implements hook_flush_caches(). */ function mymodule_flush_caches() { // After ...
  • 399
13 votes

How do I implement hook_form_alter() in a module?

Here is a working example I have pulled from a project and altered to have your module name. Add this at the top of your .module file: use Drupal\Core\Form\FormStateInterface; Then copy this in /**...
  • 13.6k
13 votes
Accepted

How to programmatically create a node on user registration with the user as author?

In an insert hook the user is already saved and should have an user id. So creating a node and assigning it to the new user as author should be no problem: /** * Implements hook_ENTITY_TYPE_insert() ...
  • 90.3k
12 votes

How know inside hook_entity_presave() if it's a create or update?

Like this: if ($node->isNew()) { } Note that this only works before saving, in insert/update, it will always return TRUE, because the node has already been saved and is no longer new.
  • 81.7k
12 votes
Accepted

Theme hooks vs module hooks

Generally speaking, only alter hooks can be implemented by themes, which means hooks like hook_form_alter() and hook_menu_alter(), or in short all those hooks that are invoked through drupal_alter() ...
  • 95k
12 votes
Accepted

Render menu in twig template

Simplify Menu sounds like what you'd want. The simplify_menu module uses a TwigExtension to gain access to Drupal's main menu's (or any other menu for that matter), render array so it can be ...
  • 26.9k
11 votes

How to run check on each page load?

I wanted to contribute something additionally to this. I am not an advocate of hook_init() or hook_boot() for doing tasks like this but tend to look for specific hooks that accomplish what you want. ...
11 votes
Accepted

How to create new field type

When I did this a while ago I adapted the excellent field_example module from the Examples Module which is always the first place to look, as it has a lot of Drupal best practice in it. As they say ...
11 votes

I can't set a default value on hook_form_alter()

According to this Documentation you can do something like $form['field_code_postal']['widget'][0]['value']['#default_value'] ='COUCOU1'; and for another guys help if you want to fill entity ...
  • 21.1k
11 votes
Accepted

What is the purpose of modulename.api.php?

An .api.php file contains the hooks that a certain module supports, and is available for reference. It offers documentation and example code on how to use the hooks in other modules. It is not ...
  • 8,239
11 votes
Accepted

Validate a custom type node before submitting

In Drupal 8 the hook hook_node_validate() has been removed. Here are some suggestions. Reference: hook_node_validate() and hook_node_submit() have been removed. Field constraints If you're trying ...
  • 90.3k
11 votes

How to use helper/private methods with a .module file?

I tend to follow Symfony standards when it comes to helper functions. So I normally add a class under src/Utils for helper methods. Then I create a service for it that I can inject other services to ...
  • 16.2k
10 votes
Accepted

Best way to delete all variables when uninstalling module

Variables are cached, so deleting manually is fine so long as you invalidate that cache, e.g. db_delete('variable') ->condition('name', 'mymodule_%', 'LIKE') ->execute(); cache_clear_all('...
  • 164k
10 votes

What's hook_node_view() alternative?

Here is the example for hook_node_view in D8 version 8.1.1 function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\...
10 votes
Accepted

How to programmatically create a title for a new node?

Here's how to do it with an entity builder callback. <?php use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeInterface; //Implements hook_form_FORM_ID_alter() function ...
  • 1,374
10 votes
Accepted

Programmatically do something when a node is updated

Hooks are still implemented as global, procedural functions; you need to declare them in the .module file just like Drupal 7, not in a controller class: function MYMODULE_entity_update(Drupal\Core\...
  • 164k
10 votes
Accepted

Override user edit page title

The title override module utilizes drupal_set_title() to do this in Drupal 7. This function is deprecated in Drupal 8. The D8 equivalent here is to alter the _title_callback for the entity.user....
  • 26.4k
10 votes

Render menu in twig template

For those who are still looking for an alternative approach, I suggest using Twig Tweak module as a more stable and more global use case, check out its cheat sheet For rendering the menu via twig ...
  • 1,554
10 votes
Accepted

How could I check that the field's value has indeed been modified?

Using hook_entity_presave(): You can get the original entity object from $entity->original when it is an update of the entity. So something like this ought to work: function foo_node_presave(\...
  • 164k
9 votes
Accepted

Where are hooks stored/cached?

The hook implementations ARE cached inside the database.. this is why it's necessary to clear cache in order for a newly implemented hook to fire. Taking a look, inside the cache_bootstrap table, ...
9 votes
Accepted

Hook_form_alter for changing field labels doesn't work of user registration page

Simple Solution just change ['0']['#title'] by [0]['value']['#title']. /** * @param $form * @param \Drupal\Core\Form\FormStateInterface $form_state * @param $form_id */ function ...
  • 6,667
8 votes

Is there a node hook that runs AFTER a node is fully inserted into the database?

There is another way this can be achievable by adding your custom callback submit function in hook_form_alter which will execute after insert/update node. E.g: Check the following code which is for ...
  • 549

Only top scored, non community-wiki answers of a minimum length are eligible