funder_field-1.1.0-alpha1/src/FunderFieldUninstallValidator.php

src/FunderFieldUninstallValidator.php
<?php

namespace Drupal\funder_field;

use Drupal\Core\Entity\EntityTypeManagerInterface;

class FunderFieldUninstallValidator implements \Drupal\Core\Extension\ModuleUninstallValidatorInterface
{

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new GroupFunderFieldUninstallValidator.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    $reasons = [];
    $items_to_delete = [];
    if ($module == 'funder_field') {
      $items_to_delete = [];
      $entityTypeManager = \Drupal::service('entity_type.manager');
      $contentTypes = $entityTypeManager->getStorage('node_type')->loadMultiple();
      foreach ($contentTypes as $contentType) {
        $type = $contentType->id();
        $fields = array_filter(
          \Drupal::service('entity_field.manager')->getFieldDefinitions('node', $type),
          function ($fieldDefinition) {
            return $fieldDefinition instanceof \Drupal\field\FieldConfigInterface;
          }
        );

        foreach ($fields as $key => $definition) {
          if($definition->getType() == "funder_field") {
            $items_to_delete[$type] = $key;
          }
        }
      }

      if(!empty($items_to_delete)) {
        $reasons[] = t("<a href='/admin/modules/uninstall/entity/funder_field'>Remove fields in order to uninstall this module</a> Note: Runs cron job for deletion");
      }
    }
    return $reasons;
  }
}

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

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