You are here

class EntityExportCsvManager in Entity Export CSV 8

Class EntityExportCsvManager.

Hierarchy

Expanded class hierarchy of EntityExportCsvManager

1 string reference to 'EntityExportCsvManager'
entity_export_csv.services.yml in ./entity_export_csv.services.yml
entity_export_csv.services.yml
1 service uses EntityExportCsvManager
entity_export_csv.manager in ./entity_export_csv.services.yml
Drupal\entity_export_csv\EntityExportCsvManager

File

src/EntityExportCsvManager.php, line 26

Namespace

Drupal\entity_export_csv
View source
class EntityExportCsvManager implements EntityExportCsvManagerInterface {
  use StringTranslationTrait;

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The current route match service.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The field type export manager.
   *
   * @var \Drupal\entity_export_csv\Plugin\FieldTypeExportManagerInterface
   */
  protected $manager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Queue factory.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\Time
   */
  protected $time;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * EntityActivityManager constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\entity_export_csv\Plugin\FieldTypeExportManagerInterface $manager
   *   The field type export manager.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info service.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Queue\QueueFactory $queue
   *   The queue factory.
   * @param \Drupal\Component\Datetime\Time $time
   *   The time service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypeExportManagerInterface $manager, AccountProxyInterface $current_user, EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher, QueueFactory $queue, Time $time, StateInterface $state) {
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
    $this->languageManager = $language_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->manager = $manager;
    $this->currentUser = $current_user;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->eventDispatcher = $event_dispatcher;
    $this->queueFactory = $queue;
    $this->time = $time;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedContentEntityTypes($return_object = TRUE) {

    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    foreach ($entity_types as $entity_type_id => $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface) {
        unset($entity_types[$entity_type_id]);
        continue;
      }
    }
    if ($return_object) {
      return $entity_types;
    }
    else {
      return array_keys($entity_types);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getContentEntityTypesEnabled($return_label = FALSE) {
    $entity_types = [];
    $entity_type_settings = $this->configFactory
      ->get('entity_export_csv.settings')
      ->get('entity_types');
    foreach ($entity_type_settings as $entity_type_id => $value) {
      if ($value['enable']) {
        if ($return_label) {
          $label = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->getEntityType()
            ->getLabel();
          $entity_types[$entity_type_id] = $label;
        }
        else {
          $entity_types[$entity_type_id] = $entity_type_id;
        }
      }
    }
    return $entity_types;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundlesPerEntityType($entity_type_id, $return_label = FALSE) {
    $options = [];
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    foreach ($bundles as $id => $bundle) {
      if ($return_label) {
        $options[$id] = $bundle['label'];
      }
      else {
        $options[$id] = $id;
      }
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundlesEnabledPerEntityType($entity_type_id, $return_label = FALSE) {
    $options = $this
      ->getBundlesPerEntityType($entity_type_id, $return_label);
    $entity_type_bundle_settings = $this
      ->getConfiguration()
      ->get('entity_types.' . $entity_type_id . '.limit_per_bundle') ?: [];
    if (!empty($entity_type_bundle_settings)) {
      $options = array_intersect_key($options, $entity_type_bundle_settings);
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleFieldDefinitions($entity_type_id, $bundle) {
    $options = [];
    $fields = $this->entityFieldManager
      ->getFieldDefinitions($entity_type_id, $bundle);
    foreach ($fields as $field_name => $field_definition) {
      $options[$field_name] = $field_definition;
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleFields($entity_type_id, $bundle, $return_field_definition = FALSE) {
    $options = [];
    $fields = $this
      ->getBundleFieldDefinitions($entity_type_id, $bundle);
    foreach ($fields as $field_name => $field_definition) {
      if ($return_field_definition) {
        $options[$field_name] = $field_definition;
      }
      else {
        $options[$field_name] = $field_definition
          ->getLabel();
      }
    }
    $event = new EntityExportCsvFieldsSupportedEvent($options, $entity_type_id, $bundle, $return_field_definition);
    $this->eventDispatcher
      ->dispatch(EntityExportCsvEvents::ENTITY_EXPORT_CSV_FIELDS_SUPPORTED, $event);
    $options = $event
      ->getFields();
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleFieldsEnabled($entity_type_id, $bundle, $return_field_definition = FALSE) {
    $options = $this
      ->getBundleFields($entity_type_id, $bundle, $return_field_definition);
    $bundle_fields_settings = $this
      ->getConfiguration()
      ->get('entity_types.' . $entity_type_id . '.bundles.' . $bundle) ?: [];
    if (!empty($bundle_fields_settings)) {
      $options = array_intersect_key($options, $bundle_fields_settings);
    }
    $event = new EntityExportCsvFieldsEnabledEvent($options, $entity_type_id, $bundle, $return_field_definition);
    $this->eventDispatcher
      ->dispatch(EntityExportCsvEvents::ENTITY_EXPORT_CSV_FIELDS_ENABLE, $event);
    $options = $event
      ->getFields();
    return $options;
  }

  /**
   * Get configuration object.
   *
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   *   The config object.
   */
  protected function getConfiguration() {
    return $this->configFactory
      ->get('entity_export_csv.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function sortNaturalFields(array &$fields, array $default_values) {
    $index = 0;
    foreach ($fields as $field_name => &$item) {
      $item = [
        $index++,
        $field_name,
        $item,
      ];
    }
    uasort($fields, function ($a, $b) use ($default_values) {
      if (isset($default_values[$a[1]]['order'], $default_values[$b[1]]['order'])) {
        if ($default_values[$a[1]]['order'] != $default_values[$b[1]]['order']) {
          return $default_values[$a[1]]['order'] < $default_values[$b[1]]['order'] ? -1 : 1;
        }
        else {
          return $a[0] < $b[0] ? -1 : 1;
        }
      }
      else {
        return $a[0] < $b[0] ? -1 : 1;
      }
    });
    foreach ($fields as &$item) {
      $item = $item[2];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigurations($entity_type_id = '') {
    $entity_export_csv = [];
    $query = $this->entityTypeManager
      ->getStorage('entity_export_csv')
      ->getQuery();
    $query
      ->condition('status', 1);
    if (!empty($entity_type_id)) {
      $query
        ->condition('entity_type_id', $entity_type_id);
    }
    $result = $query
      ->execute();
    if (!empty($result)) {
      $entity_export_csv = $this->entityTypeManager
        ->getStorage('entity_export_csv')
        ->loadMultiple($result);
    }
    return $entity_export_csv;
  }

  /**
   * {@inheritdoc}
   */
  public function getDelimiters() {
    $delimiters = [
      ',' => $this
        ->t('Comma (,)'),
      ';' => $this
        ->t('Semicolon (;)'),
      '\\t' => $this
        ->t('Tab (\\t)'),
      ':' => $this
        ->t('Colon (:)'),
      '|' => $this
        ->t('Pipe (|)'),
      '.' => $this
        ->t('Period (.)'),
      ' ' => $this
        ->t('Space ( )'),
    ];

    // @TODO Allows altering separators (Dispatch an Event).
    return $delimiters;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityExportCsvManager::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
EntityExportCsvManager::$currentRouteMatch protected property The current route match service.
EntityExportCsvManager::$currentUser protected property The current user.
EntityExportCsvManager::$entityFieldManager protected property The entity field manager.
EntityExportCsvManager::$entityRepository protected property The entity repository.
EntityExportCsvManager::$entityTypeBundleInfo protected property The entity type bundle info service.
EntityExportCsvManager::$entityTypeManager protected property The entity type manager.
EntityExportCsvManager::$eventDispatcher protected property The event dispatcher.
EntityExportCsvManager::$languageManager protected property The language manager.
EntityExportCsvManager::$manager protected property The field type export manager.
EntityExportCsvManager::$queueFactory protected property Queue factory.
EntityExportCsvManager::$state protected property The state service.
EntityExportCsvManager::$time protected property The time service.
EntityExportCsvManager::getBundleFieldDefinitions public function Get the fields definitions given an entity type and a bundle. Overrides EntityExportCsvManagerInterface::getBundleFieldDefinitions
EntityExportCsvManager::getBundleFields public function Get the fields as options given an entity type and a bundle. Overrides EntityExportCsvManagerInterface::getBundleFields
EntityExportCsvManager::getBundleFieldsEnabled public function Get the fields enabled as options given an entity type and a bundle. Overrides EntityExportCsvManagerInterface::getBundleFieldsEnabled
EntityExportCsvManager::getBundlesEnabledPerEntityType public function Gets the bundles enabled of an entity type. Overrides EntityExportCsvManagerInterface::getBundlesEnabledPerEntityType
EntityExportCsvManager::getBundlesPerEntityType public function Gets the bundles of an entity type. Overrides EntityExportCsvManagerInterface::getBundlesPerEntityType
EntityExportCsvManager::getConfiguration protected function Get configuration object.
EntityExportCsvManager::getConfigurations public function Get the entity export csv configurations. Overrides EntityExportCsvManagerInterface::getConfigurations
EntityExportCsvManager::getContentEntityTypesEnabled public function Returns an array of content entity types ID enabled. Overrides EntityExportCsvManagerInterface::getContentEntityTypesEnabled
EntityExportCsvManager::getDelimiters public function Get the delimiter options. Overrides EntityExportCsvManagerInterface::getDelimiters
EntityExportCsvManager::getSupportedContentEntityTypes public function Returns objects or id of content entity types supported. Overrides EntityExportCsvManagerInterface::getSupportedContentEntityTypes
EntityExportCsvManager::sortNaturalFields public function Sort the fields given the fields configuration default value order. Overrides EntityExportCsvManagerInterface::sortNaturalFields
EntityExportCsvManager::__construct public function EntityActivityManager constructor.
EntityExportCsvManagerInterface::PRIVATE_DIRECTORY constant The private export directory.
EntityExportCsvManagerInterface::TEMPORARY_DIRECTORY constant The temporary export directory.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.