Hot answers tagged

3

To attach a library only when a node in a certain view mode is displayed you can use hook_entity_view/hook_ENTITY_TYPE_view like following. /** * Implements hook_ENTITY_TYPE_view(). */ function MYMODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) { ...


3

hook_entity_presave() is called for every entity being saved which means that, since that hook_entity_presave() implementation is saving an entity, it is being invoked twice. Using method_exists() is generally wrong, as in PHP is generally preferable to check if the object is an instance of a class implementing an interface or a class extending another ...


2

hook_entity_presave() runs when any entity is inserted or saved; thus, it runs twice in your case, since you have a Node and a Webform entity being saved. You would have to add a conditional to check the entity type as @berramou's answer suggests. Alternatively, you could use hook_ENTITY_TYPE_presave() and target the node. /* * Implements ...


2

If you want the hook_entity_presave() to only focus on nodes and not on webforms, you can check the entity type like in the following code. /** * Implements hook_entity_presave(). */ function YourModule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { if ($entity->getEntityTypeId() == 'node') { // Do you webform creation code here ...


1

You need to get the values first you want to work with. For example the node object is inside $variables['elements']['#node']. In the sample below field_foobar is a text field. Depending on what your field type is you may need to get the value you want a little bit differently. And also maybe sanitize it first before you use it in a template suggestion (...


1

To check for error codes you can use the following snippet. It won't work for 200 status codes though. Since there's no exception. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; $exception = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'); if ($exception instanceof HttpExceptionInterface) { $...


1

There is no hook fired in delete of block, as you can see in the list of block hooks here block.api.php. There is a topic in Drupal forum here. I think the best way to handle this is by hook_form_alter something like. /** * Implements hook_form_alter(). */ function YourModule_form_alter(&$form, $form_state, $form_id) { if ($form_id == '...


1

I have this to check that the users has remaining downloads or redirect them to an upgrade page. /** * Implements hook_file_download(). */ function mymodule_file_download($uri) { // Check if user is logged in if (\Drupal::currentUser()->isAuthenticated()) { // Load entity of current user $current_user = \Drupal\user\Entity\User::load(\...


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