class FieldClonerForm in Scheduled Updates 8
Class FieldClonerForm.
@package Drupal\scheduled_updates\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\scheduled_updates\Form\FieldClonerForm uses ClassUtilsTrait, FieldUtilsTrait
Expanded class hierarchy of FieldClonerForm
1 string reference to 'FieldClonerForm'
File
- src/
Form/ FieldClonerForm.php, line 28 - Contains \Drupal\scheduled_updates\Form\FieldClonerForm.
Namespace
Drupal\scheduled_updates\FormView source
class FieldClonerForm extends FormBase {
use ClassUtilsTrait;
use FieldUtilsTrait;
/**
* Drupal\Core\Entity\EntityFieldManager definition.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entity_field_manager;
/**
* @var ScheduledUpdateTypeInterface
*/
protected $entity;
/**
* @var \Drupal\scheduled_updates\FieldManagerInterface
*/
protected $fieldManager;
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
public function __construct(EntityFieldManager $entity_field_manager, FieldManagerInterface $fieldManager, EntityTypeManagerInterface $entityTypeManager) {
$this->entity_field_manager = $entity_field_manager;
$this->fieldManager = $fieldManager;
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('scheduled_updates.field_manager'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'scheduled_updates_field_cloner_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ScheduledUpdateTypeInterface $scheduled_update_type = NULL) {
$this->entity = $scheduled_update_type;
$form += $this
->createFieldsElements($form_state);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Clone Fields'),
];
return $form;
}
/**
* Create field elements for all field on the entity type to update.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array
*/
protected function createFieldsElements(FormStateInterface $form_state) {
$base_options = [];
/** @var FieldStorageDefinitionInterface[] $config_fields */
$config_fields = [];
$entity_type = $this->entity
->getUpdateEntityType();
$target_entity_label = $this
->targetTypeLabel($this->entity);
$target_bundle_label = $this
->targetTypeBundleLabel($this->entity);
$elements = [
'#type' => 'container',
];
$destination_fields = $this
->getDestinationFields($entity_type);
$map = $this->entity
->getFieldMap();
foreach ($destination_fields as $destination_field) {
$field_name = $destination_field
->getName();
if (!in_array($field_name, $map)) {
if ($destination_field
->isBaseField()) {
$base_options[$field_name] = $destination_field
->getLabel();
}
else {
$config_fields[] = $destination_field;
}
}
}
$elements['base_fields'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Base Fields'),
'#options' => $base_options,
'#description' => $this
->t('These fields are available on all @label entities and may not be configurable.', [
'@label' => $target_entity_label,
]),
];
$elements['config_fields'] = [
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => $this
->t('Configurable Fields'),
];
if ($this
->targetSupportBundles($this->entity)) {
$elements['config_fields']['#description'] = $this
->t('These fields have been added to different @bundle_label bundles of @entity_label. They may not be on all @entity_label entities.', [
'@entity_label' => $target_entity_label,
'@bundle_label' => $target_bundle_label,
]);
}
else {
$elements['config_fields']['#description'] = $this
->t('These fields have been added to @entity_label entities.', [
'@entity_label' => $target_entity_label,
]);
}
foreach ($config_fields as $config_field) {
$instances = $this->fieldManager
->getAllFieldConfigsForField($config_field, $entity_type);
if ($instances) {
$instance_options = [
'' => $this
->t('(Don\'t clone)'),
];
foreach ($instances as $bundle => $instance) {
$instance_options[$instance
->id()] = $this
->t('As it is configured in @bundle as @label', [
'@bundle' => $bundle,
'@label' => $instance
->getLabel(),
]);
}
$elements['config_fields'][$config_field
->getName()] = [
'#type' => 'select',
'#title' => $config_field
->getLabel(),
'#options' => $instance_options,
];
}
}
//$this->entity_field_manager->getFieldDefinitions();
return $elements;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$base_fields = array_filter($form_state
->getValue('base_fields'));
$new_map = [];
// Clone Base Fields
foreach ($base_fields as $base_field) {
/** @var \Drupal\field\Entity\FieldConfig $new_field */
$new_field = $this->fieldManager
->cloneField($this->entity, $base_field);
$new_map[$new_field
->getName()] = $base_field;
}
// Clone Configurable Fields
if ($config_field_ids = $form_state
->getValue('config_fields')) {
foreach ($config_field_ids as $config_field_id) {
/** @var FieldConfig $config_definition */
if ($config_definition = FieldConfig::load($config_field_id)) {
$field_name = $config_definition
->getFieldStorageDefinition()
->getName();
$new_field = $this->fieldManager
->cloneField($this->entity, $field_name, $config_definition
->id());
$new_map[$new_field
->getName()] = $field_name;
}
}
}
if ($new_map) {
// Update Map
$this->entity
->addNewFieldMappings($new_map);
$this->entity
->save();
$this
->messenger()
->addStatus($this
->t('The fields have been created and mapped.'));
if ($this
->currentUser()
->hasPermission('administer scheduled_update form display')) {
// Redirect to form display so user and adjust settings.
$form_state
->setRedirectUrl(Url::fromRoute("entity.entity_form_display.scheduled_update.default", array(
$this->entity
->getEntityTypeId() => $this->entity
->id(),
)));
}
else {
$this
->messenger()
->addWarning($this
->t('You do not have permission to administer fields on Scheduled Updates.'));
}
}
}
function FieldManager() {
return $this->entity_field_manager;
}
function getEntity() {
return $this->entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | Determines if the class for an entity type definition implements and interface. | |
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | Get the entity owner if applicable. | |
ClassUtilsTrait:: |
protected | function | Get the revision owner for an ContentEntity. | |
ClassUtilsTrait:: |
protected | function | Determines if an object or class name implements any interfaces in a list. | |
ClassUtilsTrait:: |
protected | function | Get class names of interfaces that support revision ownership. | |
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
public | function | ||
ClassUtilsTrait:: |
protected | function | ||
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 | |
FieldClonerForm:: |
protected | property | ||
FieldClonerForm:: |
protected | property | ||
FieldClonerForm:: |
protected | property | Drupal\Core\Entity\EntityFieldManager definition. | |
FieldClonerForm:: |
protected | property | ||
FieldClonerForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
FieldClonerForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FieldClonerForm:: |
protected | function | Create field elements for all field on the entity type to update. | |
FieldClonerForm:: |
function |
Overrides FieldUtilsTrait:: |
||
FieldClonerForm:: |
function | |||
FieldClonerForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FieldClonerForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
FieldClonerForm:: |
public | function | ||
FieldUtilsTrait:: |
protected | function | ||
FieldUtilsTrait:: |
protected | function | Return all fields that can be used as destinations fields. | |
FieldUtilsTrait:: |
protected | function | ||
FieldUtilsTrait:: |
protected | function | ||
FieldUtilsTrait:: |
public | function | Get field definition on bundle. | |
FieldUtilsTrait:: |
protected | function | ||
FieldUtilsTrait:: |
protected | function | Utility Function to load a single field definition. | |
FieldUtilsTrait:: |
protected | function | Get Fields that can used as a destination field for this type. | |
FieldUtilsTrait:: |
protected | function | ||
FieldUtilsTrait:: |
protected | function | Check if a field on the entity type to update is a possible destination field. | |
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. |