New answers tagged

0

As with Drupal 8, the hooks invoked when an entity is saved (because created or updated) are the following ones: hook_ENTITY_TYPE_presave() and hook_entity_presave(), invoked before an entity (created or updated) is saved in the database hook_ENTITY_TYPE_insert() and hook_entity_insert(), invoked after a created entity has been saved in the database ...


0

function hook_entity_presave - act on an entity before it is created or updated. You can compare before and after changed field values using $entity and $entity->original entities. If your field is changed (different than original), you need to save it's processed values to your custom DB table. Simple sample, for text field called as "field_text1&...


0

Vielen dank dafür, dass war genau das was ich brauchte. Und ich habe das auch soweit hin bekommen in der node edit version: <?php use Drupal\node\NodeInterface; use Drupal\Core\Form\FormStateInterface; /** * Implements hook_ENTITY_TYPE_prepare_form() for node entities. */ function copy_fields_from_model_contract_node_edit_node_prepare_form(...


1

Use hook_ENTITY_TYPE_prepare_form if you want to change a field value before the form is built for an existing node: <?php use Drupal\node\NodeInterface; use Drupal\Core\Form\FormStateInterface; /** * Implements hook_ENTITY_TYPE_prepare_form() for node entities. */ function mymodule_node_prepare_form(NodeInterface $node, $operation, FormStateInterface ...


0

If your question isn't about the hierarchy levels but the number of nodes that can be added in any level here's a question and answer with a lot more information: How to increase book weight range for a same-level chapters You can recreate the Book features with Entity reference fields if this is a very serious limitation for you.


1

Yes, books in Drupal 8 and 9 have a maximum depth of 9, which is the value of the BookManager::BOOK_MAX_DEPTH constant. const BOOK_MAX_DEPTH = 9;


0

The answer provided by Shawn works great, but if you would prefer to have separate controllers per content type eg. PageController, ArticleController etc. This is achievable by the Kernel Subscriber. services: app.kernel_subscriber: class: Drupal\app\EventSubscriber\KernelSubscriber arguments: ['@controller_resolver'] tags: - { name: ...


1

$node->set('uid', 0); $node->save should have worked. Maybe you forgot to flush caches. To programmatically set the author to anonymous user on any entity you can call $entitiy->setOwnerId(0); and then $entity->save();, done. $entity->setOwnerId(0); $entity->save();


2

I'm assuming the Block you're using is a Views Block. You can filter out the current node with the ID Contextual filter (under Advanced Views settings) from the "Content" Category with these settings: When the filter value is NOT available Provide default value Content ID from URL When the filter value IS in the URL or a default is provided Specify ...


0

In Drupal 9 (and a while before), the changed field of the node entity is of type ChangedItem (see BaseFieldDefinition::create('changed') in Node.php). In Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem, you'll find the following: public function preSave() { parent::preSave(); ... if (!$entity->isNew() && $original && $...


0

I guess you are using Entity metadata wrapper solution to set values of an entity (in this case node) , if so you have to use set method like this : $location->field_locationmap->set([$form_state['values']['field_locationmap']['lon'], $form_state['values']['field_locationmap']['lat']]); if not using entity metadata wrapper you might using some code ...


0

You could use Block Class module to set blocks with some useful classes like "only-role_name", etc. and hide/display it on your theme stylesheet. Maybe even you could set a new block type and, if this type has its own css class (I'm not sure about it), use it to hide/display using, again, your theme stylesheet. You could, too, use a new content ...


1

user_cancel() is run in a batch. This means that either it's called in a form, or batch_process() needs to be called after it. For example, this is the code used from UserController::confirmCancel(). $edit = [ 'user_cancel_notify' => isset($account_data['cancel_notify']) ? $account_data['cancel_notify'] : $this->config('user.settings')->get('...


0

Ok i have resolved this using a database query and a custom block. Pre-req: Installing visitors module. Then having this query in the custom clock public function build() { $node = \Drupal::request()->attributes->get('node'); $uidValue = $node->getOwnerId(); $urlValue = $node->toUrl()->toString(); $database = \Drupal::database(); **$...


Top 50 recent answers are included