New answers tagged

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

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


0

As mentioned by @Clive, breadcrumbs are not entities so I was not be able to get it done by adding a mytheme_theme_suggestions_breadcrumb_alter. Instead to solve the problem I had to do the following. On top of this, I ended up downloading and enabling Easy Breadcrumb to further modify the Breadcrumb Copy the breadcrumb.html.twig file from the Classy theme ...


0

I needed to do something similar - this worked for me: function your_module_node_insert($node) { if ($node->getType() == 'your_node') { $node->setTitle('New entity ' . $node->id()); // to update title with node id $node->field_test = 'Id' . $node->id(); // or, to update a text field with node id $node->save(); }...


0

I needed to do something similar - this worked for me: I needed to do something similar - this worked for me: function your_module_node_insert($node) { if ($node->getType() == 'your_node') { //$node->type did not seem to work $node->setTitle('New entity ' . $node->id()); // to update title with node id $node->field_test = '...


0

I had the same problem, i solved implementing the hook_schema and hook_uninstall in this way /** * Implements hook_uninstall(). */ function my_module_uninstall() { drupal_static( 'my_module_uninstall', true ); } /** * Implements hook_schema(). */ function my_module_schema() { if( drupal_static( 'my_module_uninstall', false ) ){ return ...


0

If someone is looking to change the field view mode then I have managed this something like below code. I'm using a taxonomy term view and inside term name field I have chosen Rendered Entity formatter and default view mode. In some case I want to change the view mode so that I can show another design. I made one condition like below use use Drupal\views\...


0

You can do it that way too: in Oriented object way don't call the method like 'my_api_authentication' it should be [$this, 'myApiAuthentication'] and also the method should be Camel Cases, try with the following: class MyFormEventsSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [...


1

Have your POST code inside a validate function, that runs last, after all other validation has passed. Ex: $form['submit']['#validate'][] = 'mymodule_post_to_api'; and use form_set_error('form', 'Failed to connect to API, try again later'); inside your catch. For POST's errors, don't wait for someone to report. Programmatically make Drupal send yourself ...


1

You can attach custom tags to any render array from where it bubbles up into the HTML head. In the following sample I'm adding structured data when a certain block is viewed. $value then contains what's being printed between the opening and closing script tag. /** * Implements hook_block_view_BASE_BLOCK_ID_alter(). */ function ...


0

I have not been able to figure out how to conditionally change field to link using hook_views_pre_render(), but I did figure out how to do it within the Views UI: Fields->Parent ID->Rewrite results->Override the output of this field with custom text: {% if parent_type == 'paragraph' %} <a href="/find-paragraphs?id={{ parent_id }}">{{ parent_id }}</...


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.


0

Found the answer: // https://drupal.stackexchange.com/questions/272495/how-to-get-views-field-names-values-for-hook-views-pre-render foreach ($view->result as $key => $value) { //- Get parent_id value $parent_id_value = $value->_entity->parent_id->getValue()[0]['value']; //- Get parent_type value $parent_type_value = $value->...


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


0

Yes, it runs only on cache clear. You have to implement the dynamic behavior inside of the controller. By extending the core user controller you can call the parent method in case the dynamic condition doesn't apply: class UserCustomController extends UserController { public function resetPass(Request $request, $uid, $timestamp, $hash) { if (!...) { ...


Top 50 recent answers are included