getRouteName()); $entity_type = $parts[1]; $parameters = $route_match->getParameters(); // Check if one of the required parameter is missing. foreach ([$entity_type, 'field_name', 'transition_id'] as $required_parameter) { if (!$parameters->has($required_parameter)) { return AccessResult::neutral(); } } /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $route_match->getParameter($entity_type); $field_name = $route_match->getParameter('field_name'); // Ensures the passed entity has a state field matching the field name // passed in the url. if (!$entity || !$entity->hasField($field_name)) { return AccessResult::forbidden(); } /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */ $state_item = $entity->get($field_name)->first(); $allowed_transitions = array_keys($state_item->getTransitions()); // Now check if the requested transition is allowed. $requested_transition = $route_match->getParameter('transition_id'); if (!in_array($requested_transition, $allowed_transitions, TRUE)) { return AccessResult::forbidden()->addCacheableDependency($entity); } // Now finally check that the current user can update the given entity. return $entity->access('update', $account, TRUE); } }