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\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class EntityExportFormBuilder {
use StringTranslationTrait;
const SERVICE_NAME = "content_synchronizer.entity_export_form_builder";
const ARCHIVE_PARAMS = 'archive';
public static function me() {
return \Drupal::service(static::SERVICE_NAME);
}
protected $exportManager;
protected $request;
protected $currentUrl;
public function __construct(ExportManager $exportManager, RequestStack $requestStack) {
$this->exportManager = $exportManager;
$this->request = $requestStack
->getCurrentRequest();
}
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 $objectId !== NULL;
}
}
}
}
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);
}
if ($entity instanceof MenuInterface) {
$this
->addMenuExportForm($entity, $form, $formState);
}
}
protected function initExportForm(EntityInterface $entity, array &$form, FormStateInterface $formState, $isBundle = FALSE) {
$exportManager = \Drupal::service(ExportManager::SERVICE_NAME);
$form['content_synchronizer'] = [
'#type' => 'details',
'#title' => $isBundle ? $this
->t('Export all entities of @bundle bundle', [
'@bundle' => $entity
->label(),
]) : $this
->t('Export'),
'#group' => 'advanced',
'#weight' => '100',
];
$quickExportButton = $isBundle ? $this
->t('Export entities') : $this
->t('Export entity');
$addToExportButton = $isBundle ? $this
->t('Or add the entities to an existing export') : $this
->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' => $this
->t('Add to the export'),
'#submit' => [
get_called_class() . '::onAddToExport',
],
];
}
}
protected function getQuickExportUrl(EntityInterface $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;
}
protected function addMenuExportForm(MenuInterface $entity, array &$form, FormStateInterface $formState) {
$exportManager = \Drupal::service(ExportManager::SERVICE_NAME);
$form['content_synchronizer'] = [
'#type' => 'details',
'#title' => $this
->t('Export'),
'#group' => 'advanced',
'#weight' => '100',
];
$quickExportButton = $this
->t('Export entity');
$form['content_synchronizer']['add_to_export'] = [
'#type' => 'submit',
'#value' => $quickExportButton,
'#submit' => [
get_called_class() . '::onAddMenuToExport',
],
];
}
public static function onAddMenuToExport(array &$form, FormStateInterface $formState) {
$menu = $formState
->getFormObject()
->getEntity();
if ($menu instanceof Menu) {
$exportEntity = ExportEntity::create([
'name' => $menu
->label(),
]);
$exportEntity
->save();
$menuLinkTree = \Drupal::service('menu.link_tree');
$tree = $menuLinkTree
->load($menu
->id(), new MenuTreeParameters());
static::me()
->addMenuElementsToExportEntity($tree, $exportEntity);
$formState
->setRedirectUrl(Url::fromRoute('entity.export_entity.canonical', [
'export_entity' => $exportEntity
->id(),
]));
}
}
protected function addMenuElementsToExportEntity(array $tree, ExportEntity $exportEntity) {
foreach ($tree as $item) {
if (isset($item->link
->getPluginDefinition()['metadata']['entity_id'])) {
if ($menuItem = MenuLinkContent::load($item->link
->getPluginDefinition()['metadata']['entity_id'])) {
$exportEntity
->addEntity($menuItem);
}
}
if ($item->subtree && count($item->subtree)) {
$this
->addMenuElementsToExportEntity($item->subtree, $exportEntity);
}
}
}
}