function EntityListBuilder::getDefaultOperations

Gets this list's default operations.

@todo Uncomment new method parameters before drupal:12.0.0.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the operations are for. phpcs:disable Drupal.Commenting

\Drupal\Core\Cache\CacheableMetadata|null $cacheability: The cacheable metadata to add to if your operations vary by or depend on something. phpcs:enable

Return value

array The array structure is identical to the return value of self::getOperations().

See also

https://www.drupal.org/project/drupal/issues/3533078

4 calls to EntityListBuilder::getDefaultOperations()
ConfigEntityListBuilder::getDefaultOperations in core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php
Gets this list's default operations.
EntityListBuilder::getOperations in core/lib/Drupal/Core/Entity/EntityListBuilder.php
Provides an array of information to build a list of operation links.
MenuLinkListBuilder::getDefaultOperations in core/modules/menu_link_content/src/MenuLinkListBuilder.php
Gets this list's default operations.
WorkspaceListBuilder::getDefaultOperations in core/modules/workspaces/src/WorkspaceListBuilder.php
Gets this list's default operations.
3 methods override EntityListBuilder::getDefaultOperations()
ConfigEntityListBuilder::getDefaultOperations in core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php
Gets this list's default operations.
MenuLinkListBuilder::getDefaultOperations in core/modules/menu_link_content/src/MenuLinkListBuilder.php
Gets this list's default operations.
WorkspaceListBuilder::getDefaultOperations in core/modules/workspaces/src/WorkspaceListBuilder.php
Gets this list's default operations.

File

core/lib/Drupal/Core/Entity/EntityListBuilder.php, line 160

Class

EntityListBuilder
Defines a generic implementation to build a listing of entities.

Namespace

Drupal\Core\Entity

Code

protected function getDefaultOperations(EntityInterface $entity) {
  $args = func_get_args();
  $cacheability = $args[1] ?? new CacheableMetadata();
  $operations = [];
  $variables = [
    '@entity_label' => $entity->label() ?? '',
    '@entity_bundle' => $entity->bundle(),
    '@entity_id' => $entity->id(),
  ];
  $update_access = $entity->access('update', return_as_object: TRUE);
  $cacheability->addCacheableDependency($update_access);
  if ($update_access->isAllowed() && $entity->hasLinkTemplate('edit-form')) {
    $edit_url = $this->ensureDestination($entity->toUrl('edit-form'));
    $label = $entity->label() ? $this->t('Edit @entity_label', $variables) : $this->t('Edit @entity_bundle @entity_id', $variables);
    $attributes = $edit_url->getOption('attributes') ?: [];
    $attributes += [
      'aria-label' => $label,
    ];
    $edit_url->setOption('attributes', $attributes);
    $operations['edit'] = [
      'title' => $this->t('Edit'),
      'weight' => 10,
      'url' => $edit_url,
    ];
  }
  $delete_access = $entity->access('delete', return_as_object: TRUE);
  $cacheability->addCacheableDependency($delete_access);
  if ($delete_access->isAllowed() && $entity->hasLinkTemplate('delete-form')) {
    $delete_url = $this->ensureDestination($entity->toUrl('delete-form'));
    $label = $entity->label() ? $this->t('Delete @entity_label', $variables) : $this->t('Delete @entity_bundle @entity_id', $variables);
    $attributes = $delete_url->getOption('attributes') ?: [];
    $attributes += [
      'aria-label' => $label,
    ];
    $delete_url->setOption('attributes', $attributes);
    $operations['delete'] = [
      'title' => $this->t('Delete'),
      'weight' => 100,
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 880,
        ]),
      ],
      'url' => $delete_url,
    ];
  }
  $view_access = $entity->access('view', return_as_object: TRUE);
  $cacheability->addCacheableDependency($view_access);
  if ($view_access->isAllowed() && $entity->hasLinkTemplate('canonical')) {
    $view_url = $entity->toUrl('canonical');
    $label = $entity->label() ? $this->t('View @entity_label', $variables) : $this->t('View @entity_bundle @entity_id', $variables);
    $attributes = $view_url->getOption('attributes') ?: [];
    $attributes += [
      'aria-label' => $label,
    ];
    $view_url->setOption('attributes', $attributes);
    $operations['view'] = [
      'title' => $this->t('View'),
      'url' => $view_url,
      'weight' => 200,
    ];
  }
  return $operations;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.