View source
<?php
namespace Drupal\content_synchronizer\Service;
use Drupal\content_synchronizer\Entity\ExportEntity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class EntityExportFormBuilder {
const SERVICE_NAME = "content_synchronizer.entity_export_form_builder";
const ARCHIVE_PARAMS = 'archive';
protected $currentUrl;
public function addExportFields(array &$form, FormStateInterface $formState) {
if ($this
->isEntityEditForm($form, $formState)) {
$this
->addExportFieldsToEntityForm($form, $formState);
}
}
protected function isEntityEditForm(array &$form, FormStateInterface $formState) {
$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;
}
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);
}
}
protected function initExportForm(EntityInterface $entity, array &$form, FormStateInterface $formState, $isBundle = FALSE) {
$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',
];
$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',
],
];
}
}
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);
}
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)) {
foreach (ExportEntity::loadMultiple() as $export) {
foreach ($entitiesToExport as $entityToExport) {
if (array_key_exists($export
->id(), $exportsList)) {
$export
->addEntity($entityToExport);
}
}
}
}
}
else {
foreach (ExportEntity::loadMultiple() as $export) {
if (array_key_exists($export
->id(), $exportsList)) {
$export
->addEntity($entity);
}
else {
$export
->removeEntity($entity);
}
}
}
}
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;
}
}