You are here

entity_extra_field_ui.module in Entity Extra Field 8

Same filename and directory in other branches
  1. 2.0.x modules/entity_extra_field_ui/entity_extra_field_ui.module

The hook implementation for the entity extra field UI module.

File

modules/entity_extra_field_ui/entity_extra_field_ui.module
View source
<?php

/**
 * @file
 * The hook implementation for the entity extra field UI module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;

/**
 * Implements hook_entity_operation().
 */
function entity_extra_field_ui_entity_operation(EntityInterface $entity) {
  $operations = [];
  if (entity_extra_field_ui_entity_has_field_ui($entity)) {
    $account = \Drupal::currentUser();
    $entity_type = $entity
      ->getEntityType();
    $base_entity_type_id = $entity_type
      ->getBundleOf();
    if ($account
      ->hasPermission('administer entity extra field')) {
      $operations['manage-extra-fields'] = [
        'title' => t('Manage extra fields'),
        'weight' => 17,
        'url' => Url::fromRoute("entity.{$base_entity_type_id}.extra_fields", [
          $entity
            ->getEntityTypeId() => $entity
            ->id(),
        ]),
      ];
    }
  }
  return $operations;
}

/**
 * Entity has field UI.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   An entity to check if a field UI exist.
 *
 * @return bool
 *   Return TRUE if the entity has field UI; otherwise FALSE.
 *
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function entity_extra_field_ui_entity_has_field_ui(EntityInterface $entity) {
  $entity_type = $entity
    ->getEntityType();
  if (!$entity_type instanceof EntityTypeInterface || $entity_type
    ->getBundleOf() === NULL) {
    return FALSE;
  }
  $definition = \Drupal::entityTypeManager()
    ->getDefinition($entity_type
    ->getBundleOf());
  if ($definition
    ->get('field_ui_base_route') === NULL) {
    return FALSE;
  }
  return TRUE;
}