Hot answers tagged

5 votes

When adding new nodes how can I pre-populate the body field with the body of another node?

If you add the template node ID as URL query parameter /node/add/product?template=123 this code adds the body content of the template: mymodule.module: use Drupal\node\Entity\Node; /** * Implements ...
4uk4's user avatar
  • 99.2k
3 votes
Accepted

When adding new nodes how can I pre-populate the body field with the body of another node?

The Field clone module can do this without custom code: Example usage: node/add/page?fieldclone=node:17:body This module adds an option to clone fields to entity create/edit pages via get parameters. ...
Yuri's user avatar
  • 618
3 votes
Accepted

Is it logical to give a homepage an alias?

Drupal core allows to have multiple paths for the same node. In the case of a node assigned as a homepage and with an alias there are at least three paths to access the node. / /node/123 /my-alias ...
4uk4's user avatar
  • 99.2k
3 votes
Accepted

How to access the node/term field-values in a cleaner way?

You can use magic getter methods like $entity->field_name->database_column to get the first value of a field: $node->field_name->value; $node->field_entity->target_id; $node->...
Hudri's user avatar
  • 5,453
3 votes
Accepted

Statistics counter based on user role

The statistics module adds a js library to the node build. You can remove it if you don't want to count the node: mymodule.module /** * Implements hook_ENTITY_TYPE_view_alter() for node entities. */ ...
4uk4's user avatar
  • 99.2k
2 votes

Changing label on node edit form

This code is compatible with Drupal 9. The label is translatable as well. use Drupal\Core\Form\FormStateInterface; /** * Implements hook_form_alter(). */ function new_module_form_alter(&$form, ...
Priya Degwekar's user avatar
2 votes

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "node_type" plugin does not exist

Search your config files and replace every occurrence of id: node_type With id: entity_bundle:node Import updated config and clear caches, and it should work.
Webdrips's user avatar
  • 742
2 votes

How can I get a printable version of a node?

Use the Printer and PDF versions module.
cilefen's user avatar
  • 5,830
2 votes
Accepted

How to have a different hero image per node?

You are asking for a better solution, this would be a hero block instead of hard-coding the hero image in the page template. If your block is using the node route context it automatically loads the ...
4uk4's user avatar
  • 99.2k
1 vote
Accepted

How can I avoid a node is crawled and indexed by search engines in the tightest way possible?

Yes, Drupal has a mechanism to block all crawlers in the “tightest way possible”: authentication.
cilefen's user avatar
  • 5,830
1 vote
Accepted

How can I embed PHP code in HTML?

In answer to the questions you asked: No this will not work. Drupal 9 uses the Twig templating engine. You cannot execute arbitrary PHP code in Twig. Instead you should write a Drupal module.
cilefen's user avatar
  • 5,830
1 vote

How can I copy a field to another field?

You can run this, in hook_update_N(): $storage = \Drupal::entityTypeManager()->getStorage('node'); // Load all the nodes IDs for nodes that don't have the new value set. $nids = $storage->...
Jaypan's user avatar
  • 17.4k
1 vote

How can I overwrite the value of a field in a bundle class?

To query field values Views operates on SQL basis, to display you can configure a Views field plugin and additionally use the Field API field formatter. These are the two classes you should consider ...
4uk4's user avatar
  • 99.2k
1 vote

How do I programmatically assign a new book when a new node is created?

you can use a hook implementation in a custom module /** * Implements hook_node_insert(). */ function mymodule_node_insert($node) { // Check if the node is of the desired content type and no ...
Rafael's user avatar
  • 309
1 vote
Accepted

Getting old node field value in update hook

As the docs say: Get the original entity object from $entity->original. In your case that would be, for example: function custom_module_node_update(\Drupal\node\NodeInterface $node) { $...
Clive's user avatar
  • 167k
1 vote
Accepted

Append new paragraph item to existing node

After spending hours on this, I came to this solution. Currently the script retrieves existing paragraph items and adding them again together with the new paragraph item. $product_details =...
Souvik Das's user avatar
1 vote
Accepted

How to make it so theme function isn't used when editing a node with layout builder

More general for the block, if you want to make sure the theme function is only used when viewing the node and not in other node routes like editing, then check the route name first: function ...
4uk4's user avatar
  • 99.2k
1 vote

How can I set view permissions for content type to make it visible to autenticated users only?

Drupal core doesn't come with the separate "View content" permission. To get that functionality you need to search for a module that adds it. I think all the contributed modules that provide ...
prkos's user avatar
  • 7,066
1 vote
Accepted

Field values in node HTML body

It sounds like you need to use tokens. Drupal core has token functionality built in but doesn't include fields. Have a look at this module it adds token support for fields https://drupal.org/project/...
unusedspoon's user avatar
1 vote

Use AJAX On Node Add Form To Change Field Required Status

Changes to the form must be made in the form definition, and cannot be made in the ajax callback, as Drupal has already cached the form by the point of the ajax callback, so even if you figured out a ...
Jaypan's user avatar
  • 17.4k
1 vote

Display Suite ignores hook_preprocess_node()

I was able to trigger it using hook_preprocess_ds_entity_view(): function mytheme_preprocess_ds_entity_view(&$variables) { $variables['node'] = $variables['content']['#node']; $variables['...
Dave Nattriss's user avatar
1 vote

Combine two fields to be displayed together

You can combine fields in modern Drupal (8, 9, 10 etc) with Field Token Value. This module provides a field type with a widget that allows defining the value containing tokens (which can be the ...
mlncn's user avatar
  • 1,452
1 vote

Why isn't an image from the local domain shown with the explanation that, for security reasons, only images from the local domain are allowed?

I solved this by re-ordering the filters, so tracking is done before the Restriction check. Track images uploaded via a Text Editor Restrict images to this site* Image Resize Filter: Resize images ...
NickStees's user avatar
  • 111
1 vote

user_cancel_reassign() doesn't work when user has more than 10 nodes

In my case, I was performing mass user deletion in a Drush command. First I was calling directly the batch operation _user_cancel because calling user_cancel() was leading to perform a batch in a ...
romain ni's user avatar
  • 827
1 vote

How to restrict access to some nodes based on roles or users?

This is a few years old but I thought I would throw in an answer on how to do this with a very light weight custom module. This answer would be applicable to Drupal 8, 9, and 10. I am using Drupal 8 ...
serverjohn's user avatar
1 vote

Limit the number of nodes per User

To limit users from adding more than one node for a specific content type, you can use the hook hook_form_alter() to alter the node form and disable the submit button if the user has already created a ...
Dragan Petrovic FSD's user avatar
1 vote

Update a field without changing the node

The functionality is available on SqlContentEntityStorage but it's protected. Bother! said Pooh and reflected on his poor Drupal coding practices: $storage = $this->entityTypeManager->...
̷c̷h̷x̷'s user avatar

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