View source
<?php
namespace Drupal\field_tools\Form;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field_tools\DisplayCloner;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityDisplayBulkCloneForm extends FormBase {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $entityDisplayRepository;
protected $displayCloner;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository, DisplayCloner $display_cloner) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityDisplayRepository = $entity_display_repository;
$this->displayCloner = $display_cloner;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('entity_display.repository'), $container
->get('field_tools.display_cloner'));
}
public function getFormId() {
return 'field_tools_displays_clone_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
$form['displays']['entity_form_display'] = array(
'#title' => $this
->t('Form displays to clone'),
'#type' => 'checkboxes',
'#options' => $this
->getDisplayOptions('entity_form_display', $entity_type_id, $bundle),
'#description' => $this
->t("Select form displays to clone onto one or more bundles."),
);
$form['displays']['entity_view_display'] = array(
'#title' => $this
->t('View displays to clone'),
'#type' => 'checkboxes',
'#options' => $this
->getDisplayOptions('entity_view_display', $entity_type_id, $bundle),
'#description' => $this
->t("Select view displays to clone onto one or more bundles."),
);
$entity_type_bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$destination_bundle_options = [];
foreach ($entity_type_bundles as $bundle_id => $bundle_info) {
if ($bundle_id == $bundle) {
continue;
}
$destination_bundle_options[$bundle_id] = $bundle_info['label'];
}
natcasesort($destination_bundle_options);
$form['destination_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t("Bundle to clone displays to"),
'#options' => $destination_bundle_options,
];
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Clone displays'),
);
return $form;
}
protected function getDisplayOptions($type, $entity_type_id, $bundle) {
$display_ids = $this->entityTypeManager
->getStorage($type)
->getQuery()
->condition('targetEntityType', $entity_type_id)
->condition('bundle', $bundle)
->execute();
$form_displays = $this->entityTypeManager
->getStorage($type)
->loadMultiple($display_ids);
if ($type == 'entity_form_display') {
$mode_options = $this->entityDisplayRepository
->getFormModeOptions($entity_type_id);
}
else {
$mode_options = $this->entityDisplayRepository
->getViewModeOptions($entity_type_id);
}
$form_display_options = [];
foreach ($form_displays as $id => $form_display) {
$form_display_options[$id] = $mode_options[$form_display
->getMode()];
}
asort($form_display_options);
return $form_display_options;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$destination_bundles = array_filter($form_state
->getValue('destination_bundles'));
$form_display_ids = array_filter($form_state
->getValue('entity_form_display'));
$form_displays_to_clone = $this->entityTypeManager
->getStorage('entity_form_display')
->loadMultiple($form_display_ids);
foreach ($form_displays_to_clone as $form_display) {
foreach ($destination_bundles as $destination_bundle) {
$this->displayCloner
->cloneDisplay($form_display, $destination_bundle);
}
}
$view_display_ids = array_filter($form_state
->getValue('entity_view_display'));
$view_displays_to_clone = $this->entityTypeManager
->getStorage('entity_view_display')
->loadMultiple($view_display_ids);
foreach ($view_displays_to_clone as $view_display) {
foreach ($destination_bundles as $destination_bundle) {
$this->displayCloner
->cloneDisplay($view_display, $destination_bundle);
}
}
$this
->messenger()
->addMessage(t("The displays have been cloned."));
}
}