class FormatterForm in Custom Formatters 8.3
Form controller for the shortcut set entity edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\custom_formatters\Form\FormatterForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FormatterForm
File
- src/
Form/ FormatterForm.php, line 16
Namespace
Drupal\custom_formatters\FormView source
class FormatterForm extends EntityForm {
/**
* The entity being used by this form.
*
* @var \Drupal\custom_formatters\FormatterInterface
*/
protected $entity;
/**
* Formatter extras plugin manager.
*
* @var FormatterExtrasManager
*/
protected $formatterExtrasManager;
/**
* Field formatter plugin manager.
*
* @var FormatterPluginManager
*/
protected $fieldFormatterManager;
/**
* Field type plugin manager.
*
* @var FieldTypePluginManagerInterface
*/
protected $fieldTypeManager;
/**
* Constructs a FormatterForm object.
*/
public function __construct(FormatterExtrasManager $formatter_extras_manager, FormatterPluginManager $field_formatter_manager, FieldTypePluginManagerInterface $field_type_manager) {
$this->formatterExtrasManager = $formatter_extras_manager;
$this->fieldTypeManager = $field_type_manager;
$this->fieldFormatterManager = $field_formatter_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.custom_formatters.formatter_extras'), $container
->get('plugin.manager.field.formatter'), $container
->get('plugin.manager.field.field_type'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$formatter_type = $this->entity
->getFormatterType();
$form = parent::form($form, $form_state);
// Show warning if formatter is currently in use.
$dependent_entities = $this->entity
->getDependentEntities();
if ($dependent_entities) {
$form['warning'] = [
'#theme' => 'status_messages',
'#message_list' => [
'warning' => [
$this
->t("Changing the field type(s) are currently disabled as this formatter is required by the following configuration(s): @config", [
'@config' => $this
->getDependentEntitiesList($dependent_entities),
]),
],
],
'#status_headings' => [
'warning' => t('Warning message'),
],
];
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Formatter name'),
'#description' => $this
->t('This will appear in the administrative interface to easily identify it.'),
'#required' => TRUE,
'#default_value' => $this->entity
->label(),
];
$form['id'] = [
'#type' => 'machine_name',
'#machine_name' => [
'exists' => '\\Drupal\\custom_formatters\\Entity\\Formatter::load',
'source' => [
'label',
],
'replace_pattern' => '[^a-z0-9_]+',
'replace' => '_',
],
'#default_value' => $this->entity
->isNew() ? NULL : $this->entity
->id(),
'#disabled' => !$this->entity
->isNew(),
'#maxlength' => 255,
];
$form['type'] = [
'#type' => 'value',
'#value' => $this->entity
->get('type'),
];
$form['status'] = [
'#type' => 'value',
'#value' => TRUE,
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#default_value' => $this->entity
->get('description'),
];
$form['field_types'] = [
'#type' => 'select',
'#title' => $this
->t('Field type(s)'),
'#options' => $this
->getFieldTypes(),
'#default_value' => $this->entity
->get('field_types'),
'#required' => TRUE,
'#multiple' => $formatter_type
->getPluginDefinition()['multipleFields'],
'#ajax' => [
'callback' => '::formAjax',
'wrapper' => 'plugin-wrapper',
],
'#disabled' => $dependent_entities,
];
// Get Formatter type settings form.
$plugin_form = [];
$form['plugin'] = $formatter_type
->settingsForm($plugin_form, $form_state);
$form['plugin']['#type'] = 'container';
$form['plugin']['#prefix'] = "<div id='plugin-wrapper'>";
$form['plugin']['#suffix'] = "</div>";
// Third party integration settings form.
$extras = $this
->getFormatterExtrasForm();
if ($extras && is_array($extras)) {
$form['vertical_tabs'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Extras'),
'#parents' => [
'extras',
],
];
$form['extras'] = $extras;
$form['extras']['#tree'] = TRUE;
}
return $form;
}
/**
* Returns the settings form for any available third party integrations.
*/
public function getFormatterExtrasForm() {
$form = [];
$definitions = $this->formatterExtrasManager
->getDefinitions();
if (is_array($definitions) && !empty($definitions)) {
foreach ($definitions as $definition) {
$extras_form = $this->formatterExtrasManager
->invoke($definition['id'], 'settingsForm', $this->entity);
if (is_array($extras_form) && !empty($extras_form)) {
// Extras form.
$form[$definition['id']] = $extras_form;
// Extras form details element.
$form[$definition['id']]['#type'] = 'details';
$form[$definition['id']]['#title'] = $definition['label'];
$form[$definition['id']]['#description'] = $definition['description'];
$form[$definition['id']]['#group'] = 'extras';
}
}
}
return $form;
}
/**
* Ajax callback for form.
*
* @param array $form
* The form array.
* @param FormStateInterface $form_state
* The form state object.
*
* @return mixed
* The ajax form element.
*/
public function formAjax(array $form, FormStateInterface $form_state) {
return $form['plugin'];
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->getFormatterType()
->submitForm($form, $form_state);
$entity = $this->entity;
$is_new = !$entity
->getOriginalId();
// Invoke all third party integrations save method.
$this->formatterExtrasManager
->invokeAll('settingsSave', $entity, $form, $form_state);
$entity
->save();
// Clear cached formatters.
// @TODO - Tag custom formatters?
$this->fieldFormatterManager
->clearCachedDefinitions();
if ($is_new) {
$this
->messenger()
->addStatus($this
->t('Added formatter %formatter.', [
'%formatter' => $entity
->label(),
]));
}
else {
$this
->messenger()
->addStatus($this
->t('Updated formatter %formatter.', [
'%formatter' => $entity
->label(),
]));
}
$form_state
->setRedirectUrl(new Url('entity.formatter.collection'));
}
/**
* Returns a list of dependent entities.
*
* @param array $entities
* The dependent entities.
*
* @return mixed|null
* The rendered list of dependent entities.
*/
protected function getDependentEntitiesList(array $entities = []) {
$list = [];
foreach ($entities as $entity) {
$entity_type_id = $entity
->getEntityTypeId();
if (!isset($list[$entity_type_id])) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
// Store the ID and label to sort the entity types and entities later.
$label = $entity_type
->getLabel();
$list[$entity_type_id] = [
'#theme' => 'item_list',
'#title' => $label,
'#items' => [],
];
}
$list[$entity_type_id]['#items'][$entity
->id()] = $entity
->label() ?: $entity
->id();
}
return render($list);
}
/**
* Returns an array of available field types.
*
* @TODO - Allow formatter type plugin to modify this list.
*
* @return mixed
* Array of field types grouped by their providers.
*/
protected function getFieldTypes() {
$options = [];
$field_types = $this->fieldTypeManager
->getDefinitions();
$this->moduleHandler
->alter('custom_formatters_fields', $field_types);
ksort($field_types);
foreach ($field_types as $field_type) {
$options[$field_type['provider']][$field_type['id']] = $field_type['label']
->render();
}
ksort($options);
return $options;
}
}
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 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormatterForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
|
FormatterForm:: |
protected | property | Field formatter plugin manager. | |
FormatterForm:: |
protected | property | Field type plugin manager. | |
FormatterForm:: |
protected | property | Formatter extras plugin manager. | |
FormatterForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FormatterForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
FormatterForm:: |
public | function | Ajax callback for form. | |
FormatterForm:: |
protected | function | Returns a list of dependent entities. | |
FormatterForm:: |
protected | function | Returns an array of available field types. | |
FormatterForm:: |
public | function | Returns the settings form for any available third party integrations. | |
FormatterForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
FormatterForm:: |
public | function | Constructs a FormatterForm object. | |
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. |