class FieldConfigurationForm in Search API 8
Defines a form for changing a field's configuration.
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\search_api\Form\FieldConfigurationForm uses UnsavedConfigurationFormTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FieldConfigurationForm
File
- src/
Form/ FieldConfigurationForm.php, line 22
Namespace
Drupal\search_api\FormView source
class FieldConfigurationForm extends EntityForm {
use UnsavedConfigurationFormTrait;
/**
* The index for which the fields are configured.
*
* @var \Drupal\search_api\IndexInterface
*/
protected $entity;
/**
* The field whose configuration is edited.
*
* @var \Drupal\search_api\Item\FieldInterface
*/
protected $field;
/**
* The "id" attribute of the generated form.
*
* @var string
*/
protected $formIdAttribute;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a FieldConfigurationForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer to use.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, DateFormatterInterface $date_formatter, RequestStack $request_stack, MessengerInterface $messenger) {
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->dateFormatter = $date_formatter;
$this->requestStack = $request_stack;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
$renderer = $container
->get('renderer');
$date_formatter = $container
->get('date.formatter');
$request_stack = $container
->get('request_stack');
$messenger = $container
->get('messenger');
return new static($entity_type_manager, $renderer, $date_formatter, $request_stack, $messenger);
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'search_api_field_config';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$field = $this
->getField();
if (!$field) {
$args['@id'] = $this
->getRequest()->attributes
->get('field_id');
$form['message'] = [
'#markup' => $this
->t('Unknown field with ID "@id".', $args),
];
return $form;
}
$args['%field'] = $field
->getLabel();
$form['#title'] = $this
->t('Edit field %field', $args);
if ($this
->getRequest()->query
->get('modal_redirect')) {
$form['title']['#markup'] = new FormattableMarkup('<h2>@title</h2>', [
'@title' => $form['#title'],
]);
Html::setIsAjax(TRUE);
}
$this->formIdAttribute = Html::getUniqueId($this
->getFormId());
$form['#id'] = $this->formIdAttribute;
$form['messages'] = [
'#type' => 'status_messages',
];
$property = $field
->getDataDefinition();
if (!$property instanceof ConfigurablePropertyInterface) {
$args['%field'] = $field
->getLabel();
$form['message'] = [
'#markup' => $this
->t('Field %field is not configurable.', $args),
];
return $form;
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$field = $this
->getField();
/** @var \Drupal\search_api\Processor\ConfigurablePropertyInterface $property */
$property = $field
->getDataDefinition();
$form = $property
->buildConfigurationForm($field, $form, $form_state);
$this
->checkEntityEditable($form, $this->entity);
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
unset($actions['delete']);
if ($this
->getRequest()->query
->get('modal_redirect')) {
$actions['submit']['#ajax']['wrapper'] = $this->formIdAttribute;
}
else {
$actions['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => $this->entity
->toUrl('fields'),
];
}
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$field = $this
->getField();
/** @var \Drupal\search_api\Processor\ConfigurablePropertyInterface $property */
$property = $field
->getDataDefinition();
$property
->validateConfigurationForm($field, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$field = $this
->getField();
/** @var \Drupal\search_api\Processor\ConfigurablePropertyInterface $property */
$property = $field
->getDataDefinition();
$property
->submitConfigurationForm($field, $form, $form_state);
$this->messenger
->addStatus($this
->t('The field configuration was successfully saved.'));
if ($this
->getRequest()->query
->get('modal_redirect')) {
$url = $this->entity
->toUrl('add-fields-ajax')
->setOption('query', [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
]);
$form_state
->setRedirectUrl($url);
}
else {
$form_state
->setRedirectUrl($this->entity
->toUrl('fields'));
}
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// Our form structure doesn't emulate the entity structure, so copying those
// values wouldn't make any sense.
}
/**
* Retrieves the field that is being edited.
*
* @return \Drupal\search_api\Item\FieldInterface|null
* The field, if it exists.
*/
protected function getField() {
if (!isset($this->field)) {
$field_id = $this
->getRequest()->attributes
->get('field_id');
$this->field = $this->entity
->getField($field_id);
}
return $this->field;
}
}
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 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 |
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 |
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 |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
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 | ||
EntityForm:: |
public | function | ||
FieldConfigurationForm:: |
protected | property |
The index for which the fields are configured. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
protected | property | The field whose configuration is edited. | |
FieldConfigurationForm:: |
protected | property | The "id" attribute of the generated form. | |
FieldConfigurationForm:: |
protected | property |
The messenger. Overrides MessengerTrait:: |
|
FieldConfigurationForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
FieldConfigurationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FieldConfigurationForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
public | function |
Returns a string identifying the base form. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
protected | function | Retrieves the field that is being edited. | |
FieldConfigurationForm:: |
public | function |
Returns a unique string identifying the form. Overrides EntityForm:: |
|
FieldConfigurationForm:: |
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 EntityForm:: |
|
FieldConfigurationForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
FieldConfigurationForm:: |
public | function | Constructs a FieldConfigurationForm 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. | |
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:: |
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. | |
UnsavedConfigurationFormTrait:: |
protected | property | The date formatter. | |
UnsavedConfigurationFormTrait:: |
protected | property | The renderer. | |
UnsavedConfigurationFormTrait:: |
protected | function | Checks whether the given entity contains unsaved changes. | |
UnsavedConfigurationFormTrait:: |
public | function | Retrieves the date formatter. | |
UnsavedConfigurationFormTrait:: |
public | function | Retrieves the renderer. | |
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. |