class EntityDisplayBulkCloneForm in Field tools 8
Provides a form to clone displays from an entity bundle.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\field_tools\Form\EntityDisplayBulkCloneForm
Expanded class hierarchy of EntityDisplayBulkCloneForm
File
- src/
Form/ EntityDisplayBulkCloneForm.php, line 16
Namespace
Drupal\field_tools\FormView source
class EntityDisplayBulkCloneForm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The entity display repository service.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The field cloner.
*
* @var \Drupal\field_tools\DisplayCloner
*/
protected $displayCloner;
/**
* Creates a Clone instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository service.
* @param \Drupal\field_tools\DisplayCloner $display_cloner
* The display cloner.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'field_tools_displays_clone_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Get the form options for a display type.
*
* @param $type
* The entity type ID of the display type.
* @param $entity_type_id
* The target entity type ID of the displays.
* @param $bundle
* The target bundle.
*
* @return
* An array of form options.
*/
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);
// Unfortunately, getDisplayModesByEntityType() is protected :(
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) {
// The label() method of displays returns NULL always, so we get the label
// from the related mode.
$form_display_options[$id] = $mode_options[$form_display
->getMode()];
}
asort($form_display_options);
return $form_display_options;
}
/**
* {@inheritdoc}
*/
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."));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityDisplayBulkCloneForm:: |
protected | property | The field cloner. | |
EntityDisplayBulkCloneForm:: |
protected | property | The entity display repository service. | |
EntityDisplayBulkCloneForm:: |
protected | property | The entity type bundle info service. | |
EntityDisplayBulkCloneForm:: |
protected | property | The entity type manager. | |
EntityDisplayBulkCloneForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EntityDisplayBulkCloneForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityDisplayBulkCloneForm:: |
protected | function | Get the form options for a display type. | |
EntityDisplayBulkCloneForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityDisplayBulkCloneForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EntityDisplayBulkCloneForm:: |
public | function | Creates a Clone instance. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |