eav_field-2.x-dev/eav_field.module

eav_field.module
<?php

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\eav_field\Entity\EavAttributeInterface;
use Drupal\eav_field\Entity\EavValueInterface;
use Drupal\field\FieldConfigInterface;

/**
 * Implements hook_theme().
 */
function eav_field_theme(): array {
  return [
    'eav_list' => [
      'variables' => [
        'values' => [],
        'host_entity' => NULL,
        'field_name' => NULL,
        'only_primary' => FALSE,
        'context' => [],
      ],
    ],
  ];
}

/**
 * Preprocess function for eav-list.html.twig.
 */
function eav_field_preprocess_eav_list(array &$variables): void {
  $eav_values = &$variables['values']; /** @var EavValueInterface[] $eav_values */

  // Sort values
  uasort($eav_values, function (EavValueInterface $a, EavValueInterface $b) {
    $a_weight = ($a_attribute = $a->getAttributeEntity()) ? $a_attribute->getWeight() : 0;
    $b_weight = ($b_attribute = $b->getAttributeEntity()) ? $b_attribute->getWeight() : 0;

    if ($a_weight == $b_weight) {
      return 0;
    }

    return ($a_weight < $b_weight) ? -1 : 1;
  });

  foreach ($eav_values as $delta => $eav_value) {
    if ($attribute = $eav_value->getAttributeEntity()) {
      $attribute->configureValueFieldDefinition();

      if ($eav_value->getValueFieldItems()->isEmpty()) {
        continue;
      }
      if ($variables['only_primary'] && !$attribute->isPrimary()) {
        continue;
      }

      $variables['items'][$delta]['attribute_name'] = $attribute->getMachineName();
      $variables['items'][$delta]['attribute_label'] = $attribute->label();
      $variables['items'][$delta]['value_type'] = $attribute->getValueType();
      $variables['items'][$delta]['value'] = $eav_value->viewValueField();
    }
  }
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function eav_field_theme_suggestions_eav_list(array $variables): array {
  return [
    'eav_list__' . trim($variables['context']['view_mode'], '_'),
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 *
 * @see system_theme_suggestions_field()
 */
function eav_field_theme_suggestions_field_alter(array &$suggestions, array $variables): void {
  if ($variables['element']['#entity_type'] == 'eav_value' && strpos($variables['element']['#field_name'], '_value')) {
    $suggestions[] = $variables['theme_hook_original'] . '__eav_value__value';
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete(): eav_attribute.
 */
function eav_field_eav_attribute_delete(EavAttributeInterface $attribute): void {
  $unwanted_eav_values_ids = \Drupal::entityQuery('eav_value')
    ->condition('aid', $attribute->id())
    ->accessCheck(FALSE)
    ->execute();

  if ($unwanted_eav_values_ids) {
    $queue = \Drupal::queue('delete_eav_value_entities');
    $queue->createQueue();

    foreach ($unwanted_eav_values_ids as $unwanted_eav_value_id) {
      $queue->createItem(['vid' => $unwanted_eav_value_id]);
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert(): field_config.
 */
function eav_field_field_config_insert(FieldConfigInterface $field_config): void {
  // Rebuild routes for work local task "Edit EAV attributes"
  if ($field_config->getFieldStorageDefinition()->getType() == 'eav') {
    drupal_flush_all_caches();
  }
}

/**
 * Return entity object from current route.
 */
function eav_field_get_entity_from_route_match(RouteMatchInterface $route_match): ?FieldableEntityInterface {
  foreach ($route_match->getParameters() as $parameter) {
    if ($parameter instanceof FieldableEntityInterface) {
      return $parameter;
    }
  }

  return NULL;
}

/**
 * "edit_eav" route access check.
 */
function eav_field_edit_eav_route_access_check(string $field_name, RouteMatchInterface $route_match): AccessResultInterface {
  $entity = eav_field_get_entity_from_route_match($route_match);
  return AccessResult::allowedIf($entity->hasField($field_name));
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc