Hot answers tagged

5

I think you cannot have a module hook executed the same hook implemented by a theme. If we look at the hook invoking code, we see that the theme hooks are invoked separately and after the module hooks. The best bet would be to remove it from the theme and put it in a module, or trying with #after_build. FormBuilder.php contains the following code. $this-&...


5

For access control on routes, you can write a service which implements AccessInterface. At its simplest, this is just a class placed in a module's src/Access folder with a single method public function access(AccountInterface $account), returning a flavour of AccessResult. Then you simply register your class in the module's services.yml, annotate the route ...


4

The easiest way would be to execute the View again in the page preprocess hook. If you want to avoid executing the View twice the task is getting a bit more tricky. This is because in D8 page elements are not necessarily rendered downstream in the order you expect. The element could have been cached in a previous request or in case the element doesn't ...


4

Hooks are plain PHP functions that follow a specific name schema. As such, the same function name cannot be used from two modules. Furthermore, Drupal is not able to understand if c_a_b() is the implementation of hook_a_b() done from c.module, or the implementation of hook_b() done from c_a.module. To answer the question, yes, there would be conflicts and (...


4

Where should I place this complex access control in a way that I can easily call it in several controllers and hooks? Is there a name for the kind of feature I'm going to program? The term/concept you're looking for is a Service: Drupal 8 introduces the concept of services to decouple reusable functionality and makes these services pluggable and ...


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


3

Hello try this ... $form['field_prestation_walk']['#states'] = [ 'visible' => [ ':input[name="field_prestation_type"]' => [ 'value' => 'walk' ], ] ];


3

$form['#validate'] will not work as that form "This form works by submitting the hash and timestamp to the user.reset" aka redirects you to the user.reset from what you can do is something like temp replace this form with a custom one though. however i don't really see the point /** * Implements hook_form_alter(). */ function yourmodule_form_alter(&...


3

Lists like views will not be passed through hook_node_access(), as this would create too much overhead. Views does a query on the database to get the results. hook_node_access() is run in PHP, so these won't work (easily/well/efficently) together. To deal with lists, you will need to create a grant system, using hook_node_access_records() and ...


2

This is a late answer based on another question flagged as duplicate of this one. The Comment entity has some helpful methods for getting back the entity it's associated with. CommentInterface::getCommentedEntity should get the entity object that the comment is attached to. So if ($entity instanceof \Drupal\comment\CommentInterface) { $...


2

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


2

I had the same issue, but i was using {{ dump() }} to debug the template The problem I have the same arguments when i am using {{ dump() }} inside macro tags. For example: {% macro menu_links(items, attributes, menu_level ) %} {{ dump() }} ... code ... {% endmacro %} If you focus on function menu_links(), you realize that variables ...


2

I gave up on hook_views_pre_render and wound up meeting my requirements with template_preprocess_image_formatter as follows: function <TEMPLATE_NAME>_preprocess_image_formatter(&$variables) { $styles = array('thumbnail', 'medium', 'large'); if ($variables['image']['#style_name'] === 'medium') { $variables['image']['#style_name'] = $styles[...


2

The code that is provided by the page that you linked to is wrong. While this hook exists in D7 and while it is possible that during early D8 core development, in alpha or beta, it existed at one point. In D8 there is no such thing as hook_prepare. As per the list of all D8 core hooks, it looks to me that hook got replaced with a newcomer hook called ...


2

There might be hundreds of ways to add this markup in Drupal. The two simplest that came to my mind are: 1) Add a suffix to the form tag in form build or form alter: $form['#suffix'] = '<div>Suffix to form</div>'; 2) or add the markup in a form.html.twig below the form tag: {# /** * @file * Default theme implementation for a 'form' element. ...


2

Where my confusion comes in is when I go to specify parameters or document the function. What parameters does the function take? What hook does it implement? Preprocess hooks are only provided a single parameter: $variables. The hook that you're implementing is literally hook_preprocess_HOOK. The HOOK part of it is the identifier for the theme entry, as ...


2

A feature module is just another module. And you can enable it programmatically using the linked code from another enabled module's hook_update_N placed in this another enabled module's *.install file. This even can be another feature module. Then have this hook fired via drush updb on the command line or by visiting your site's /update.php. You also could ...


2

No performance issue concerning the node entity, which is loaded only once per request and then stored in memory. For caching between requests there are also no performance issues, because the rendered twig template(s) of a block are cached by default. But you have to make sure you provide the correct cache data, which is possible via a plugin context in ...


2

The Prepopulate module appears to be what you're looking for.


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


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

You should use hook_node_presave if you want to change a field's value as the hook is invoked from node_save() before the node is saved to the database. And as leymannx stated, make sure you clear the cache before implementing a new hook.


2

You don't need multiple ajax callbacks, you need multiple ajax commands. First wrap each form element in a separate div container and then return for each one an ajax command from the same callback: use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; ... function example_ajax_callback(&$form, FormStateInterface $form_state) { ...


2

First of all, User module's user.private_tempstore and user.shared_tempstore services moved to core. So we want to use the tempstore.private. The code below will get your started. you will want to read more about how getSubscribedEvents() works, as here is where you tweak the where and when of your event. Don't forget an .info.yml file! /mymodule/mymodule....


2

As start, the code shown in the question is wrong in many points, in particular because it is using Drupal 7 code that has not been adapted for Drupal 8. Drupal core has modules that alter the user edit form, and that code should be taken as guide for your own code. Since you want to alter the user edit form for when users create an account and when an ...


1

Is there any way to tell PHPCS that the function is an AJAX-callback (so that such warnings are generated)? Yes, it's as simple as implementing hooks except that instead of "Implements hook_", it starts with "Implements callback_". The only problem is that currently (August 2019) it is not supported by Drupal's PHPCS standard and you don't get any warnings ...


1

You need to have the allowed values settings in the following format: allowed_values: - value: '1' label: A - value: '2' label: 'Bb' - value: '3' label: Ccccc The quotes don't matter for single word strings. I hope this helps.


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