You are here

class EntityExportFormBuilder in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Service/EntityExportFormBuilder.php \Drupal\content_synchronizer\Service\EntityExportFormBuilder
  2. 3.x src/Service/EntityExportFormBuilder.php \Drupal\content_synchronizer\Service\EntityExportFormBuilder

The entity export form builder.

Hierarchy

Expanded class hierarchy of EntityExportFormBuilder

3 files declare their use of EntityExportFormBuilder
content_synchronizer.module in ./content_synchronizer.module
Hooks definitions for content_synchronizer module.
LaunchExportForm.php in src/Form/LaunchExportForm.php
QuickExportController.php in src/Controller/QuickExportController.php
1 string reference to 'EntityExportFormBuilder'
content_synchronizer.services.yml in ./content_synchronizer.services.yml
content_synchronizer.services.yml
1 service uses EntityExportFormBuilder
content_synchronizer.entity_export_form_builder in ./content_synchronizer.services.yml
\Drupal\content_synchronizer\Service\EntityExportFormBuilder

File

src/Service/EntityExportFormBuilder.php, line 17

Namespace

Drupal\content_synchronizer\Service
View source
class EntityExportFormBuilder {
  const SERVICE_NAME = "content_synchronizer.entity_export_form_builder";
  const ARCHIVE_PARAMS = 'archive';

  /**
   * The current url.
   *
   * @var \Drupal\Core\Url
   */
  protected $currentUrl;

  /**
   * Add the export submform in the entity edition form, if the entity is exportable.
   */
  public function addExportFields(array &$form, FormStateInterface $formState) {
    if ($this
      ->isEntityEditForm($form, $formState)) {
      $this
        ->addExportFieldsToEntityForm($form, $formState);
    }
  }

  /**
   * Return true if the form needs to have an export field.
   *
   * @param array $form
   *   The form build array.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The formState array.
   *
   * @return bool
   *   The result.
   */
  protected function isEntityEditForm(array &$form, FormStateInterface $formState) {

    /** @var \Drupal\Core\Entity\EntityForm $formObject */
    $formObject = $formState
      ->getFormObject();
    if ($formObject instanceof EntityForm) {
      if (in_array($formObject
        ->getOperation(), [
        'edit',
        'default',
      ])) {
        $entity = $formObject
          ->getEntity();
        if (strpos(get_class($entity), 'content_synchronizer') === FALSE) {
          if ($objectId = $entity
            ->id()) {
            return isset($objectId);
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * Add exports fields to the entity form.
   *
   * @param array $form
   *   The form build array.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The form state.
   */
  protected function addExportFieldsToEntityForm(array &$form, FormStateInterface $formState) {
    $entity = $formState
      ->getFormObject()
      ->getEntity();
    $isBundle = $entity instanceof ConfigEntityBundleBase;
    if ($entity instanceof ContentEntityBase || $isBundle) {
      $this
        ->initExportForm($entity, $form, $formState, $isBundle);
    }
  }

  /**
   * Init the export form.
   */
  protected function initExportForm(EntityInterface $entity, array &$form, FormStateInterface $formState, $isBundle = FALSE) {

    /** @var ExportManager $exportManager */
    $exportManager = \Drupal::service(ExportManager::SERVICE_NAME);
    $form['content_synchronizer'] = [
      '#type' => 'details',
      '#title' => $isBundle ? t('Export all entities of @bundle bundle', [
        '@bundle' => $entity
          ->label(),
      ]) : t('Export'),
      '#group' => 'advanced',
      '#weight' => '100',
    ];

    // Init labels.
    $quickExportButton = $isBundle ? t('Export entities') : t('Export entity');
    $addToExportButton = $isBundle ? t('Or add the entities to an existing export') : t('Or add the entity to an existing export');
    $form['content_synchronizer']['quick_export'] = [
      '#markup' => '<a href="' . $this
        ->getQuickExportUrl($entity) . '" class="button button--primary">' . $quickExportButton . '</a>',
    ];
    $exportsListOptions = $exportManager
      ->getExportsListOptions();
    if (!empty($exportsListOptions)) {
      $form['content_synchronizer']['exports_list'] = [
        '#type' => 'checkboxes',
        '#title' => $addToExportButton,
        '#options' => $exportsListOptions,
        '#default_value' => array_keys($exportManager
          ->getEntitiesExport($entity)),
      ];
      $form['content_synchronizer']['add_to_export'] = [
        '#type' => 'submit',
        '#value' => t('Add to the choosen export'),
        '#submit' => [
          get_called_class() . '::onAddToExport',
        ],
      ];
    }
  }

  /**
   * Get the batch URL.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   THe entity to download.
   *
   * @return string
   *   THe batch url.
   */
  protected function getQuickExportUrl(Entity $entity) {
    $url = Url::fromRoute('content_synchronizer.quick_export');
    $parameters = [
      'destination' => \Drupal::request()
        ->getRequestUri(),
      'entityTypeId' => $entity
        ->getEntityTypeId(),
      'entityId' => $entity
        ->id(),
    ];
    return $url
      ->toString() . '?' . http_build_query($parameters);
  }

  /**
   * Add entity to an existing entity export.
   *
   * @param array $form
   *   The form build array.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The form state.
   */
  public static function onAddToExport(array &$form, FormStateInterface $formState) {
    $exportsList = ExportEntity::loadMultiple($formState
      ->getValue('exports_list'));
    $entity = $formState
      ->getFormObject()
      ->getEntity();
    if ($entity instanceof ConfigEntityBundleBase) {
      if ($entitiesToExport = self::getEntitiesFromBundle($entity)) {

        /** @var \Drupal\content_synchronizer\Entity\ExportEntity $export */
        foreach (ExportEntity::loadMultiple() as $export) {
          foreach ($entitiesToExport as $entityToExport) {
            if (array_key_exists($export
              ->id(), $exportsList)) {
              $export
                ->addEntity($entityToExport);
            }
          }
        }
      }
    }
    else {

      /** @var \Drupal\content_synchronizer\Entity\ExportEntity $export */
      foreach (ExportEntity::loadMultiple() as $export) {
        if (array_key_exists($export
          ->id(), $exportsList)) {
          $export
            ->addEntity($entity);
        }
        else {
          $export
            ->removeEntity($entity);
        }
      }
    }
  }

  /**
   * Get the list of entities from a bundle entity.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityBundleBase $entity
   *   The bundle entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]|null
   *   The entities of the bundle.
   */
  public static function getEntitiesFromBundle(ConfigEntityBundleBase $entity) {
    $entityType = $entity
      ->getEntityType()
      ->getBundleOf();
    $bundleKey = \Drupal::entityTypeManager()
      ->getDefinitions()[$entityType]
      ->getKeys()['bundle'];
    $query = \Drupal::entityQuery($entityType)
      ->condition($bundleKey, $entity
      ->id());
    $entitiesIds = $query
      ->execute();
    if (!empty($entitiesIds)) {
      return \Drupal::entityTypeManager()
        ->getStorage($entityType)
        ->loadMultiple($entitiesIds);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityExportFormBuilder::$currentUrl protected property The current url.
EntityExportFormBuilder::addExportFields public function Add the export submform in the entity edition form, if the entity is exportable.
EntityExportFormBuilder::addExportFieldsToEntityForm protected function Add exports fields to the entity form.
EntityExportFormBuilder::ARCHIVE_PARAMS constant
EntityExportFormBuilder::getEntitiesFromBundle public static function Get the list of entities from a bundle entity.
EntityExportFormBuilder::getQuickExportUrl protected function Get the batch URL.
EntityExportFormBuilder::initExportForm protected function Init the export form.
EntityExportFormBuilder::isEntityEditForm protected function Return true if the form needs to have an export field.
EntityExportFormBuilder::onAddToExport public static function Add entity to an existing entity export.
EntityExportFormBuilder::SERVICE_NAME constant