New answers tagged

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

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) { $...


0

I think you should send render of the block not an array of block entity. View Method return an array. Try with renderer service like the following: /** * Implements hook__preprocess_HOOK(). */ function mymodule_preprocess_node(&$variables) { $node = $variables['elements']['#node']; if ($node->bundle() == 'page') { $block_entity = Drupal\...


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 == '...


0

I was trying to do something similar as well. I had an integer field and I wanted to replace the integer column with a string value. That was not working. So I had to add a separate string field, maintained the integer field as a hidden field and then overwrote the string field. The code that worked is given below. function module_views_pre_render(...


0

I just used query_alter in module.views_execution.inc in a Drupal 8.7 installation and it ran fine. So there shouldn't be any change for the behavior of module.views_execution.inc location. The API documentation seems to show the same as well https://api.drupal.org/api/drupal/core%21modules%21user%21user.views_execution.inc/8.8.x


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(\...


0

I've built a lazy load module for drupal 8. Should work out-of-the-box: Fancyload - lazy load images pinterest-style Fancyload will automatically provide lazyloading of images on your website in a pinterest-style color scheme. It fetches the main color of your image and serves it until your image is loaded.


0

I don't think you can increase the length of a field within Drupal once there is data in the field. Instead, go into your entity bundle and make a new field with a larger character limit. Then, copy the data from the old field to the new field. There are several ways to accomplish this (programmatically with PHP; directly in the database with SQL queries; ...


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) { ...


0

Drupal does not distinct between full and default here. If no full viewmode is given for a certain node type than the default viewmode is used. So you could check whether a given node type implements the full viewmode and apply your conditional logic based on this, like this: function hook_page_attachments(array &$attachments) { /* @var \Drupal\node\...


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 (...


Top 50 recent answers are included