Skip to content
Snippets Groups Projects
Commit a5bdff5c authored by Sander Wind's avatar Sander Wind
Browse files

Use FieldUiHooks::entityOperation() to keep it DRY

parent dd304d08
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\field_ui\Hook\FieldUiHooks;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupTypeInterface;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface;
......@@ -58,82 +59,41 @@ class OperationProvider implements OperationProviderInterface {
$ui_allowed = !$this->groupRelationType->isEnforced() && !$this->groupRelationType->isCodeOnly();
if ($relationship_type_id = $this->getRelationshipTypeId($group_type)) {
$route_params = ['group_relationship_type' => $relationship_type_id];
$operations['configure'] = [
'title' => $this->t('Configure'),
'url' => new Url('entity.group_relationship_type.edit_form', $route_params),
];
$route_params = ['group_relationship_type' => $relationship_type_id];
$operations['configure'] = [
'title' => $this->t('Configure'),
'url' => new Url('entity.group_relationship_type.edit_form', $route_params),
];
if ($ui_allowed) {
$operations['uninstall'] = [
'title' => $this->t('Uninstall'),
'weight' => 99,
'url' => new Url('entity.group_relationship_type.delete_form', $route_params),
];
}
if ($ui_allowed) {
$operations['uninstall'] = [
'title' => $this->t('Uninstall'),
'weight' => 99,
'url' => new Url('entity.group_relationship_type.delete_form', $route_params),
];
}
// Use a safer alternative to field_ui_entity_operation()
if ($this->moduleHandler->moduleExists('field_ui')) {
$relationship_type = $this->entityTypeManager()->getStorage('group_relationship_type')->load($relationship_type_id);
if ($relationship_type) {
$operations += $this->getFieldUiOperations($relationship_type);
}
// Use a safer alternative to field_ui_entity_operation()
if ($this->moduleHandler->moduleExists('field_ui')) {
$relationship_type = $this->entityTypeManager()->getStorage('group_relationship_type')->load($relationship_type_id);
if ($relationship_type) {
$operations += new FieldUiHooks()->entityOperation($relationship_type);
}
}
}
}
elseif ($ui_allowed) {
$operations['install'] = [
'title' => $this->t('Install'),
'url' => new Url('entity.group_relationship_type.add_form', [
'group_type' => $group_type->id(),
'plugin_id' => $this->pluginId,
]),
];
$operations['install'] = [
'title' => $this->t('Install'),
'url' => new Url('entity.group_relationship_type.add_form', [
'group_type' => $group_type->id(),
'plugin_id' => $this->pluginId,
]),
];
}
return $operations;
}
/**
* Generates Field UI operations.
*/
protected function getFieldUiOperations(EntityInterface $entity): array {
$operations = [];
$entity_type_id = $entity->getEntityTypeId();
// Ensure we only add Field UI operations for entities that support it.
if (in_array($entity_type_id, ['node', 'taxonomy_term', 'user', 'group'])) {
$route_name = "entity.{$entity_type_id}.field_ui_fields";
if ($this->currentUser->hasPermission('administer ' . $entity_type_id . ' fields')) {
$operations['manage_fields'] = [
'title' => $this->t('Manage fields'),
'url' => Url::fromRoute($route_name, [$entity_type_id => $entity->id()]),
];
}
}
return $operations;
}
/**
* Provides entity operation links, replacing field_ui_entity_operation().
*/
protected function getEntityOperations($entity) {
$operations = [];
if ($entity->hasLinkTemplate('edit-form')) {
$operations['edit'] = [
'title' => $this->t('Edit'),
'url' => $entity->toUrl('edit-form'),
];
}
if ($entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = [
'title' => $this->t('Delete'),
'url' => $entity->toUrl('delete-form'),
];
}
return $operations;
}
/**
* {@inheritdoc}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment