diff --git a/src/Plugin/Field/FieldType/PathFieldItemList.php b/src/Plugin/Field/FieldType/PathFieldItemList.php new file mode 100644 index 0000000..c592408 --- /dev/null +++ b/src/Plugin/Field/FieldType/PathFieldItemList.php @@ -0,0 +1,14 @@ +getEntity(); + $entity_type_id = $entity->getEntityTypeId(); + $bundle_id = $entity->bundle(); + $entity_type = $entity->getEntityType(); + + if (!$entity_type instanceof EntityTypeInterface) { + return $default_access; + } + + $permission = NULL; + switch ($entity_type->getPermissionGranularity()) { + case 'bundle': + $permission = "edit $bundle_id $entity_type_id url alias"; + break; + + case 'entity_type': + $permission = "edit $entity_type_id url alias"; + break; + } + + if ($permission === NULL) { + return $default_access; + } + + return $default_access->orIf( + AccessResult::allowedIfHasPermission($account, $permission)->cachePerPermissions() + ); + } + +} diff --git a/src/Plugin/Field/FieldType/PathautoFieldItemList.php b/src/Plugin/Field/FieldType/PathautoFieldItemList.php new file mode 100644 index 0000000..e14258a --- /dev/null +++ b/src/Plugin/Field/FieldType/PathautoFieldItemList.php @@ -0,0 +1,14 @@ +' . t('Grant access to users to edit and create path aliases for each entity type.') . '

'; } } /** - * Implements hook_field_widget_complete_form_alter(). + * Implements hook_module_implements_alter(). */ -function url_alias_permissions_field_widget_complete_form_alter(array &$field_widget_complete_form, FormStateInterface $form_state, array $context) { - if (!$form_state->getFormObject() instanceof EntityFormInterface) { - return; - } - - // Check if the items element isset. - if (!isset($context['items']) || !$context['items'] instanceof FieldItemListInterface) { - return; - } - - $field_definition = $context['items']->getFieldDefinition(); - // Check if the field is of the type 'path'. - if ($field_definition->getType() !== 'path') { - return; - } - - $entity = $context['items']->getEntity(); - $entity_type_id = $entity->getEntityTypeId(); - $bundle_id = $entity->bundle(); - $entity_type = $entity->getEntityType(); - - if (!$entity_type instanceof EntityTypeInterface) { - return; - } - - $permission = NULL; - switch ($entity_type->getPermissionGranularity()) { - case 'bundle': - $permission = "edit $bundle_id $entity_type_id url alias"; - break; - - case 'entity_type': - $permission = "edit $entity_type_id url alias"; - break; +function url_alias_permissions_module_implements_alter(&$implementations, $hook) { + // Move our field_info_alter hook to the end of the list. + if ($hook === 'field_info_alter') { + $group = $implementations['url_alias_permissions']; + unset($implementations['url_alias_permissions']); + $implementations['url_alias_permissions'] = $group; } +} - if ($permission === NULL) { - return; +/** + * Implements hook_field_info_alter(). + */ +function url_alias_permissions_field_info_alter(&$info) { + if (isset($info['path'])) { + $module_handler = \Drupal::service('module_handler'); + $info['path']['list_class'] = PathFieldItemList::class; + + if ($module_handler->moduleExists('pathauto')) { + $info['path']['list_class'] = PathautoFieldItemList::class; + } } - - $field_widget_complete_form['#access'] = \Drupal::currentUser()->hasPermission($permission); }