Während die Hooks ausgelöst werden, werden die Absätze nicht aktualisiert. Protokolle zeigen manchmal Meldungen wie:
- „Keine Absätze gefunden“
- „Knoten ist keiner Gruppe zugeordnet“< /li>
- Muss ich Entitäten (wie Absätze oder Gruppen) in diesen Hooks neu laden, um sicherzustellen, dass die Daten aktuell sind?
- Gibt es irgendwelche Macken mit „hook_entity_insert“ oder „hook_entity_update“, die dieses Verhalten verursachen könnten?
- Könnte das Problem bei mir liegen? Benutzerdefinierte Hilfsfunktionen zum Abrufen Referenzierte Entitäten oder Gruppendaten?
Code: Select all
/**
* Implements hook_entity_insert().
*/
function my_module_entity_insert(EntityInterface $entity) {
if ($entity instanceof NodeInterface) {
my_module_handle_event_paragraph_tagging($entity);
}
}
/**
* Implements hook_entity_update().
*/
function my_module_entity_update(EntityInterface $entity) {
if ($entity instanceof NodeInterface) {
my_module_handle_event_paragraph_tagging($entity);
}
}
/**
* Handles tagging for paragraphs of type 'event' in a given node.
*
* @param \Drupal\node\NodeInterface $entity
* The node entity being saved. This node contains referenced paragraphs.
*/
function my_module_handle_event_paragraph_tagging(NodeInterface $entity) {
if (!$entity->hasField('field_content_function')) {
\Drupal::logger('my_module')->warning('Node @id does not have field_content_function.', ['@id' => $entity->id()]);
return;
}
$paragraphs = FieldHelper::getReferencedEntities($entity, 'field_content_function');
if (empty($paragraphs)) {
\Drupal::logger('my_module')->notice('No paragraphs found in field_content_function for node @id.', ['@id' => $entity->id()]);
return;
}
$group = EntityHelper::getGroup($entity);
if (!$group) {
\Drupal::logger('my_module')->warning('Node @id is not associated with a group.', ['@id' => $entity->id()]);
return;
}
$auto_main_tag_groups = FieldHelper::getReferencedEntities($group, 'field_auto_main_tagged_groups') ?? [];
$auto_main_tag_group_ids = array_map(fn($g) => $g->id(), $auto_main_tag_groups);
foreach ($paragraphs as $paragraph) {
if ($paragraph->bundle() !== 'event') {
continue;
}
$main_event_list_groups = FieldHelper::getReferencedEntities($paragraph, 'field_main_tagged_groups') ?? [];
$main_event_list_group_ids = array_map(fn($g) => $g->id(), $main_event_list_groups);
$groups_to_add = array_diff($auto_main_tag_group_ids, $main_event_list_group_ids);
foreach ($groups_to_add as $group_id) {
$paragraph->get('field_main_tagged_groups')->appendItem(['target_id' => $group_id]);
}
try {
$paragraph->save();
} catch (\Exception $e) {
\Drupal::logger('my_module')->error('Error saving paragraph @id: @message', [
'@id' => $paragraph->id(),
'@message' => $e->getMessage(),
]);
}
}
}
/**
* Retrieves the group associated with a node.
*
* @param \Drupal\node\NodeInterface $node
* The node entity.
*
* @return \Drupal\group\Entity\Group|null
* The associated group entity, or NULL if no group is found.
*/
function my_module_get_group(NodeInterface $node) {
$group = FieldHelper::getReferencedEntities($node, 'field_group');
if (!empty($group)) {
return reset($group);
}
if ($node->hasField('field_parent_node')) {
$parent = FieldHelper::getReferencedEntities($node, 'field_parent_node');
if (!empty($parent) && $parent[0] instanceof NodeInterface) {
return my_module_get_group($parent[0]);
}
}
\Drupal::logger('my_module')->warning('No group found for node @id.', ['@id' => $node->id()]);
return NULL;
}