You are here

function field_tools_entity_operation in Field tools 8

Implements hook_entity_operation().

File

./field_tools.module, line 78
field_tools.module Contains useful tools for working with fields.

Code

function field_tools_entity_operation(\Drupal\Core\Entity\EntityInterface $entity) {
  $operations = [];
  if ($entity
    ->getEntityTypeId() == 'field_config') {
    $target_entity_type_id = $entity
      ->getTargetEntityTypeId();
    $parameters = [
      'field_config' => $entity
        ->id(),
    ];

    // Because we're not going via entity URIs but direct to the route, we
    // don't get the handling from FieldConfig::urlRouteParameters(), so
    // need to do it ourselves.
    $entity_type = \Drupal::entityTypeManager()
      ->getDefinition($target_entity_type_id);
    $bundle_parameter_key = $entity_type
      ->getBundleEntityType() ?: 'bundle';
    $parameters[$bundle_parameter_key] = $entity
      ->getTargetBundle();

    // Field clone operation.
    $operations['clone'] = array(
      'title' => t('Clone'),
      'url' => \Drupal\Core\Url::fromRoute("entity.field_config.{$target_entity_type_id}_field_tools_clone_form", $parameters),
      'weight' => 50,
    );
  }

  // Add operation links to bundle entity lists for the tools tabs.
  $bundle_of_entity_type_id = $entity
    ->getEntityType()
    ->getBundleOf();
  if ($bundle_of_entity_type_id) {
    $bundle_of_entity_type = \Drupal::entityTypeManager()
      ->getDefinition($bundle_of_entity_type_id);
    if ($bundle_of_entity_type
      ->get('field_ui_base_route')) {
      $entity_type_id = $entity
        ->getEntityTypeId();
      $parameters = [
        $entity_type_id => $entity
          ->id(),
      ];
      $operations['clone_fields'] = array(
        'title' => t('Clone fields'),
        'url' => \Drupal\Core\Url::fromRoute("field_tools.field_bulk_clone_{$bundle_of_entity_type_id}", $parameters),
        'weight' => 50,
      );
    }
  }
  return $operations;
}