A link parent entity resolver plugin can now also implement \Drupal\linkchecker\Plugin\LinkChildEntityResolverInterface. Then the plugin finds the child entities of an entity, one level down. This is the opposite direction of getParentEntity().
Link Checker uses the child entities when the published status of an entity changes and the Search published contents only setting is selected. A save without a new revision does not save the child entities again. Thus Link Checker adds the child entities to its queue, and it extracts or removes their links.
The Paragraphs resolver implements the interface. Link Checker does not follow other entity references. A module must implement the interface for each relation that contains child entities. A shared entity, for example a media item that more than one node uses, is not a child entity.
namespace Drupal\my_module\Plugin\LinkParentEntityResolver;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\linkchecker\Attribute\LinkParentEntityResolver;
use Drupal\linkchecker\Plugin\LinkChildEntityResolverInterface;
use Drupal\linkchecker\Plugin\LinkParentEntityResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
#[LinkParentEntityResolver(
id: 'my_entity',
label: new TranslatableMarkup('My entity'),
entity_types: ['my_entity'],
)]
class MyEntityParentEntityResolver extends PluginBase implements LinkParentEntityResolverInterface, LinkChildEntityResolverInterface, ContainerFactoryPluginInterface {
public function __construct(array $configuration, $plugin_id, $plugin_definition, protected EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'));
}
public function getParentEntity(FieldableEntityInterface $entity, string $langcode): ?FieldableEntityInterface {
$parent = $entity->getHostEntity();
return $parent instanceof FieldableEntityInterface ? $parent : NULL;
}
public function getChildEntities(FieldableEntityInterface $entity): array {
$storage = $this->entityTypeManager->getStorage('my_entity');
// The host ID is the same for all translations of the host.
$ids = $storage->getQuery()
->accessCheck(FALSE)
->condition('host_type', $entity->getEntityTypeId())
->condition('host_id', $entity->id())
->execute();
return array_values($storage->loadMultiple($ids));
}
}
An implementation of getChildEntities() must obey these rules:
- Return saved, loaded fieldable entities. Return an empty array if the plugin does not apply to the entity.
- Return the child entities of all translations of the entity, not only of the active translation.
- Use the field values of the entity object that Link Checker gives, if the entity references its children. Then an earlier revision returns the child entities of that revision.
- Do not do access checks.
LinkParentEntityResolverManager::getChildEntityResolvers() returns the plugins that implement the interface, in the sequence of their weight.