Skip to content
Snippets Groups Projects
Verified Commit aa0e0a12 authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3356967 by nterbogt, larowlan: Route permissions have trouble with context

parent 15c0bd4e
No related branches found
No related tags found
No related merge requests found
Showing
with 52 additions and 58 deletions
......@@ -12,7 +12,7 @@ services:
arguments: ['@entity_field.manager', '@entity_type.bundle.info']
access_check.entity_hierarchy.has_fields:
class: Drupal\entity_hierarchy\Routing\ReorderChildrenAccess
arguments: ['@entity_hierarchy.information.parent_candidate', '@current_route_match', '@entity_type.manager']
arguments: ['@entity_hierarchy.information.parent_candidate', '@entity_type.manager']
tags:
- { name: access_check }
entity_hierarchy.entity_tree_node_mapper:
......
......@@ -3,8 +3,8 @@
namespace Drupal\entity_hierarchy_microsite;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeMenuItemOverrideInterface;
......
......@@ -107,7 +107,7 @@ class MicrositeMenuItemForm extends ContentEntityForm {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
$entity = parent::buildEntity($form, $form_state);
list(, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
[, $parent] = explode(':', $form_state->getValue('menu_parent'), 2);
$entity->parent->value = $parent;
$entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
......
......@@ -5,8 +5,8 @@ namespace Drupal\entity_hierarchy_microsite;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Menu\MenuTreeStorage;
use Drupal\Core\Menu\Form\MenuLinkDefaultForm;
use Drupal\Core\Menu\MenuTreeStorage;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
......
......@@ -2,9 +2,9 @@
namespace Drupal\entity_hierarchy_microsite\Plugin\Block;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_hierarchy_microsite\Plugin\MicrositePluginTrait;
use Drupal\node\NodeInterface;
......
......@@ -118,6 +118,9 @@ class EntityHierarchy extends AccessControlHierarchyBase implements ContainerFac
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'bundles' => [],
......@@ -195,7 +198,7 @@ class EntityHierarchy extends AccessControlHierarchyBase implements ContainerFac
'description' => '',
];
$tree[$parent][$id] = $entry;
$this->cacheBackend->set($cid, $entry,Cache::PERMANENT, $entry_tags);
$this->cacheBackend->set($cid, $entry, Cache::PERMANENT, $entry_tags);
$tags = array_merge($tags, $entry_tags);
}
}
......@@ -387,4 +390,5 @@ class EntityHierarchy extends AccessControlHierarchyBase implements ContainerFac
$this->configuration['bundles'] = $bundles;
return $changed;
}
}
......@@ -12,7 +12,7 @@ use Symfony\Component\Validator\Constraint;
* label = @Translation("Valid hierarchy selection", context = "Validation"),
* )
*/
class ValidEntityHierarchySection extends Constraint {
class ValidEntityHierarchySection extends Constraint {
/**
* Violation message. Use the same message as FormValidator.
......
......@@ -2,17 +2,17 @@
namespace Drupal\entity_hierarchy_workbench_access\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
/**
* Defines a class for validating entity hierarchy selection with access.
......
......@@ -11,7 +11,6 @@ use Drupal\node\Entity\Node;
use Drupal\Tests\entity_hierarchy\Kernel\EntityHierarchyKernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\workbench_access\Entity\AccessScheme;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
/**
* Tests interaction between entity_hierarchy and workbench_access.
......@@ -74,7 +73,7 @@ class EntityHierarchyWorkbenchAccessTest extends EntityHierarchyKernelTestBase {
$this->installConfig(['node', 'workbench_access']);
$this->installSchema('node', ['node_access']);
$this->parentNodeType = $this->createContentType(['type' => 'section']);
$this->childNodeType = $this->createContentType(['type' => 'children']);
$this->childNodeType = $this->createContentType(['type' => 'children']);
// Only the child has the field.
$this->setupEntityHierarchyField(static::ENTITY_TYPE, $this->childNodeType->id(), static::FIELD_NAME);
$this->scheme = AccessScheme::create([
......@@ -136,7 +135,7 @@ class EntityHierarchyWorkbenchAccessTest extends EntityHierarchyKernelTestBase {
*/
protected function doCreateTestEntity(array $values) {
$entity = Node::create([
'title' => isset($values['title']) ? $values['title'] : $this->randomMachineName(),
'title' => $values['title'] ?? $this->randomMachineName(),
'type' => $this->childNodeType->id(),
'status' => 1,
'uid' => 1,
......
......@@ -31,9 +31,9 @@ class EntityHierarchyCommands extends DrushCommands {
* Rebuild tree.
*
* @param string $field_name
* Field machine name
* Field machine name.
* @param string $entity_type_id
* Entity type id
* Entity type id.
*
* @usage drush entity-hierarchy:rebuild-tree field_parents node
* Rebuild tree for node field named field_parents.
......
......@@ -3,11 +3,11 @@
namespace Drupal\entity_hierarchy\Form;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Entity\ContentEntityForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -216,16 +216,16 @@ class HierarchyChildrenForm extends ContentEntityForm {
$handler = new $handlerClass();
$links = [];
foreach($childBundles[$fieldName] as $id => $info) {
foreach ($childBundles[$fieldName] as $id => $info) {
$url = $handler->getAddChildUrl($entityType, $this->entity, $id, $fieldName);
if($url->access()) {
if ($url->access()) {
$links[$id] = [
'title' => $this->t('Create new @bundle', ['@bundle' => $info['label']]),
'url' => $url
'url' => $url,
];
}
}
if(count($links) > 1) {
if (count($links) > 1) {
$actions['add_child'] = [
'#type' => 'dropbutton',
'#links' => $links,
......
......@@ -4,9 +4,9 @@ namespace Drupal\entity_hierarchy\Plugin\Field\FieldType;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\entity_hierarchy\Storage\InsertPosition;
use Drupal\entity_hierarchy\Storage\NestedSetStorage;
use Drupal\entity_hierarchy\Storage\TreeLockTrait;
......@@ -249,7 +249,7 @@ class EntityReferenceHierarchy extends EntityReferenceItem {
->condition($key, $ids, 'IN')
->execute();
$weightSeparator = $fieldDefinition instanceof BaseFieldDefinition ? '__' : '_';
$entities = array_combine(array_column($entities, $key), array_column($entities, $parentField . $weightSeparator. 'weight'));
$entities = array_combine(array_column($entities, $key), array_column($entities, $parentField . $weightSeparator . 'weight'));
foreach ($siblings as $node) {
if (!isset($entities[$node->getRevisionId()])) {
continue;
......
......@@ -2,9 +2,9 @@
namespace Drupal\entity_hierarchy\Plugin\Field\FieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
......@@ -78,7 +78,7 @@ class EntityReferenceHierarchyAutocomplete extends EntityReferenceAutocompleteWi
if ($this->getSetting(self::HIDE_WEIGHT)) {
$widget['weight'] = [
'#type' => 'value',
'#value' => isset($items[$delta]->weight) ? $items[$delta]->weight : 0,
'#value' => $items[$delta]->weight ?? 0,
];
}
else {
......
......@@ -4,8 +4,8 @@ namespace Drupal\entity_hierarchy\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Select widget.
......@@ -34,13 +34,13 @@ class EntityReferenceHierarchySelect extends OptionsWidgetBase {
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
return parent::settingsForm($form, $form_state) + [
'hide_weight' => [
'#type' => 'checkbox',
'#title' => $this->t('Hide weight field'),
'#description' => $this->t('Hide the weight field and use the default value instead'),
'#default_value' => $this->getSetting(self::HIDE_WEIGHT),
],
];
'hide_weight' => [
'#type' => 'checkbox',
'#title' => $this->t('Hide weight field'),
'#description' => $this->t('Hide the weight field and use the default value instead'),
'#default_value' => $this->getSetting(self::HIDE_WEIGHT),
],
];
}
/**
......@@ -62,7 +62,7 @@ class EntityReferenceHierarchySelect extends OptionsWidgetBase {
$element += [
'#type' => 'select',
'#options' => $this->getOptions($items->getEntity()),
'#default_value' => isset($items[$delta]->target_id) ? $items[$delta]->target_id : '',
'#default_value' => $items[$delta]->target_id ?? '',
];
$widget = [
......@@ -73,7 +73,7 @@ class EntityReferenceHierarchySelect extends OptionsWidgetBase {
if ($this->getSetting(self::HIDE_WEIGHT)) {
$widget['weight'] = [
'#type' => 'value',
'#value' => isset($items[$delta]->weight) ? $items[$delta]->weight : 0,
'#value' => $items[$delta]->weight ?? 0,
];
}
else {
......@@ -119,10 +119,10 @@ class EntityReferenceHierarchySelect extends OptionsWidgetBase {
// and a 'select a value' option for required fields that do not come
// with a value selected.
if (!$this->required) {
return t('- None -');
return $this->t('- None -');
}
if (!$this->has_value) {
return t('- Select a value -');
return $this->t('- Select a value -');
}
}
......
......@@ -123,7 +123,7 @@ class ValidHierarchyReferenceConstraintValidator extends ConstraintValidator imp
$has_revisions = $this_entity->getEntityType()->hasKey('revision');
$descendant_entities = \Drupal::entityQuery($this_entity->getEntityTypeId())
->condition($this_entity->getEntityType()->getKey('id'), $descendant_nested_set_node_ids, 'IN')
->accessCheck(false)
->accessCheck(FALSE)
->execute();
$descendant_entities = array_flip($descendant_entities);
foreach ($descendant_nested_set_nodes as $descendant_nested_set_node) {
......
......@@ -37,7 +37,7 @@ class HierarchyIsSiblingOfEntity extends EntityHierarchyArgumentPluginBase {
$depth_token => $node->getDepth() + 1,
];
if (!$this->options['show_self']) {
$self_token = ':self' . $this->tableAlias;
$self_token = ':self' . $this->tableAlias;
$expression .= " AND $this->tableAlias.id != {$self_token}";
$arguments[$self_token] = $stub->getId();
}
......
......@@ -86,7 +86,7 @@ class HierarchyTreeSummary extends FieldPluginBase {
'#type' => 'select',
'#required' => TRUE,
'#options' => [
'child_counts' => 'Number of children at each level below',
'child_counts' => $this->t('Number of children at each level below'),
],
'#title' => $this->t('Summary type'),
'#default_value' => $this->options['summary_type'],
......
......@@ -9,7 +9,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
......@@ -17,13 +16,6 @@ use Symfony\Component\Routing\Route;
*/
class ReorderChildrenAccess implements AccessCheckInterface {
/**
* Route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Parent candidate service.
*
......@@ -43,11 +35,10 @@ class ReorderChildrenAccess implements AccessCheckInterface {
*
* @param \Drupal\entity_hierarchy\Information\ParentCandidateInterface $parentCandidate
* Parent candidate service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager service.
*/
public function __construct(ParentCandidateInterface $parentCandidate, RouteMatchInterface $routeMatch, EntityTypeManagerInterface $entityTypeManager) {
$this->routeMatch = $routeMatch;
public function __construct(ParentCandidateInterface $parentCandidate, EntityTypeManagerInterface $entityTypeManager) {
$this->parentCandidate = $parentCandidate;
$this->entityTypeManager = $entityTypeManager;
}
......@@ -64,17 +55,17 @@ class ReorderChildrenAccess implements AccessCheckInterface {
*
* @param \Symfony\Component\Routing\Route $route
* Route being access checked.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The parametrized route.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, Request $request = NULL, AccountInterface $account = NULL) {
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
$entity_type = $route->getOption(EntityHierarchyRouteProvider::ENTITY_HIERARCHY_ENTITY_TYPE);
$entity = $this->routeMatch->getParameter($entity_type);
$entity = $route_match->getParameter($entity_type);
if (empty($entity)) {
return AccessResult::forbidden();
......
......@@ -38,7 +38,7 @@ class EntityTreeNodeMapper implements EntityTreeNodeMapperInterface {
$loadedEntities = new \SplObjectStorage();
foreach ($nodes as $node) {
$nodeId = $node->getId();
$entity = isset($entities[$nodeId]) ? $entities[$nodeId] : FALSE;
$entity = $entities[$nodeId] ?? FALSE;
if (!$entity || ($entity->getEntityType()->hasKey('revision') && $node->getRevisionId() != $entity->getRevisionId())) {
// Bypass non default revisions and deleted items.
continue;
......
......@@ -32,7 +32,7 @@ class TreeRebuilder {
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* Entity field manager
* Entity field manager.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entityTypeManager;
......@@ -202,7 +202,7 @@ class TreeRebuilder {
$sets = [];
$base_field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
$weight_separator = isset($base_field_definitions[$field_name]) ? '__' : '_';
foreach ($records as $ix => $item) {
foreach ($records as $item) {
$parent = $item["$field_name{$weight_separator}target_id"];
$sets[$parent][] = $item[$idKey];
$items[$item[$idKey]] = $parent;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment