Skip to content
Snippets Groups Projects
Commit 9eb4492b authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

Ensure that all related path aliases and menu links are transitioned automatically if needed.

parent 104b104f
Branches
Tags 2.0.0-alpha9
No related merge requests found
......@@ -4,12 +4,15 @@ namespace Drupal\entity_workflow_content\Plugin\WorkflowType;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\entity_workflow\WorkflowType\EntityWorkflowTypeBase;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\workflows\TransitionInterface;
use Drupal\workflows\WorkflowInterface;
use Drupal\workspaces\WorkspaceAssociationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @WorkflowType(
......@@ -28,6 +31,20 @@ use Drupal\workflows\WorkflowInterface;
*/
class EntityWorkflowContent extends EntityWorkflowTypeBase {
/**
* The workspace association.
*/
protected WorkspaceAssociationInterface $workspaceAssociation;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->workspaceAssociation = $container->get('workspaces.association');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -152,19 +169,33 @@ class EntityWorkflowContent extends EntityWorkflowTypeBase {
// for nodes, etc.
$related_entities = [];
// Add the entity's path alias.
foreach ($entity->getFieldDefinitions() as $field => $field_definition) {
if ($field_definition->getType() === 'path' && ($path_alias_id = $entity->get($field)->get(0)->pid)) {
$related_entities[] = PathAlias::load($path_alias_id);
}
// Add the entity's path aliases.
if ($entity->hasLinkTemplate('canonical') && $this->entityTypeManager->hasDefinition('path_alias')) {
$path = $entity->toUrl('canonical', ['path_processing' => FALSE])->toString();
$aliases = $this->entityTypeManager
->getStorage('path_alias')
->loadByProperties(['path' => $path]);
$related_entities = array_merge($related_entities, $aliases);
}
// Add the menu links for nodes.
if ($entity->getEntityTypeId() === 'node' && $this->entityTypeManager->hasDefinition('menu_link_content')) {
$menu_links = $this->entityTypeManager
->getStorage('menu_link_content')
->loadByProperties(['link.uri' => 'entity:node/' . $entity->id()]);
$related_entities = array_merge($related_entities, $menu_links);
}
// Add the menu link for nodes.
if ($entity->getEntityTypeId() === 'node') {
$menu_defaults = menu_ui_get_menu_link_defaults($entity);
if ($menu_link_id = $menu_defaults['entity_id']) {
$related_entities[] = MenuLinkContent::load($menu_link_id);
}
// Ensure that we only auto-transition related entities that are already
// tracked by the current workspace.
if ($workspace = $this->workspaceManager->getActiveWorkspace()) {
$tracked_entities = $this->workspaceAssociation->getTrackedEntities($workspace->id());
$related_entities = array_filter($related_entities, function(EntityInterface $entity) use ($tracked_entities) {
return isset($tracked_entities[$entity->getEntityTypeId()])
&& in_array($entity->id(), $tracked_entities[$entity->getEntityTypeId()]);
});
}
foreach ($related_entities as $entity) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment