class FilterConditionForm in Tooltip Taxonomy 8
Form handler for the FilterCondition entity add and 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\tooltip_taxonomy\Form\FilterConditionForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FilterConditionForm
File
- src/
Form/ FilterConditionForm.php, line 25
Namespace
Drupal\tooltip_taxonomy\FormView source
class FilterConditionForm extends EntityForm {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Drupal Condition plugin for path.
*
* @var \Drupal\Core\Condition\ConditionPluginBase
*/
protected $pathCondition;
/**
* Drupal condition plugin for content type.
*
* @var \Drupal\Core\Condition\ConditionPluginBase
*/
protected $contentTypeCondition;
/**
* The field type plugin manager.
*
* @var \Drupal\Core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypePluginManager;
/**
* The field type manager.
*
* @var \Drupal\tooltip_taxonomy\Services\FieldTypeManager
*/
protected $fieldTypeManager;
/**
* Constructs an FilterConditionForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entityTypeManager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* Entity type bundle info service instance.
* @param \Drupal\Component\Plugin\Factory\FactoryInterface $plugin_factory
* Plugin factory service instance.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager
* Field type plugin manager service instance.
* @param \Drupal\tooltip_taxonomy\Services\FieldTypeManager $field_type_manager
* Field type manager service instance.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entity_type_bundle_info, FactoryInterface $plugin_factory, FieldTypePluginManagerInterface $field_type_plugin_manager, FieldTypeManager $field_type_manager) {
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->pathCondition = $plugin_factory
->createInstance('request_path');
$this->contentTypeCondition = $plugin_factory
->createInstance('node_type');
$this->fieldTypePluginManager = $field_type_plugin_manager;
$this->fieldTypeManager = $field_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('plugin.manager.condition'), $container
->get('plugin.manager.field.field_type'), $container
->get('tooltip_taxonomy.field_type_manager'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$condition = $this->entity;
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#maxlength' => 255,
'#default_value' => $condition
->label(),
'#description' => $this
->t("Name for the filter condition."),
'#required' => TRUE,
];
$form['cid'] = [
'#type' => 'machine_name',
'#default_value' => $condition
->id(),
'#machine_name' => [
'exists' => [
$this,
'exist',
],
'source' => [
'name',
],
],
'#disabled' => !$condition
->isNew(),
];
// Vocabularies of taxonomy terms.
$voca_options = [];
foreach ($this->entityTypeBundleInfo
->getBundleInfo('taxonomy_term') as $voc_name => $voc_info) {
$voca_options[$voc_name] = $voc_info['label'];
}
$form['vids'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Vocabularies for tooltips.'),
'#options' => $voca_options,
'#default_value' => $condition
->get('vids'),
'#required' => TRUE,
'#description' => $this
->t('Selected vocabularies will be used as the content text of tooltips in following pages or content types for a certain field. If none is checked, there will not be tooltip created.'),
];
$formats = filter_formats();
$format_options = [];
foreach ($formats as $format) {
$format_options[$format
->id()] = $format
->label();
}
$form['formats'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Text formats.'),
'#options' => $format_options,
'#default_value' => $condition
->get('formats'),
'#required' => TRUE,
'#description' => $this
->t('Select which formats does this condition applys to.'),
];
// Get all view modes.
$view_mode_ids = \Drupal::entityQuery('entity_view_mode')
->condition('targetEntityType', 'node')
->execute();
$view_modes = \Drupal::entityTypeManager()
->getStorage('entity_view_mode')
->loadMultiple($view_mode_ids);
$view_mode_options = [];
foreach ($view_modes as $mode) {
$id = $mode
->id();
$view_mode_options[substr($id, 5)] = $mode
->label();
}
// View mode settings.
$form['view'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('View modes'),
'#options' => $view_mode_options,
'#default_value' => $condition
->get('view'),
'#description' => $this
->t('If none is selected, this condition will be applied to all view modes.'),
];
// Load the default value for path.
$this->pathCondition
->setConfiguration($condition
->get('path'));
// Build the form element for path settings.
$form['path'] = $this->pathCondition
->buildConfigurationForm([], $form_state);
// Content types applied to this condition.
$this->contentTypeCondition
->setConfiguration($condition
->get('contentTypes'));
// Build the form element for content type settings.
$form['node'] = $this->contentTypeCondition
->buildConfigurationForm([], $form_state);
// Remove the negate checkbox for content type setting.
unset($form['node']['negate']);
// All existing text fields that can be used for a node.
$fields = $this
->getExistingFieldStorageOptions();
if (!empty($fields)) {
// Fields applied to this condition.
$form['field'] = [
'#type' => 'select',
'#title' => $this
->t('Field'),
'#options' => $fields,
'#default_value' => $condition
->get('field'),
'#empty_option' => $this
->t('- Select an existing field -'),
'#multiple' => TRUE,
'#description' => $this
->t('If none is selected, this condition will be applied to all fields.'),
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$condition = $this->entity;
$this->pathCondition
->submitConfigurationForm($form['path'], $form_state);
$this->contentTypeCondition
->submitConfigurationForm($form['node'], $form_state);
// The content type negate should be always false.
$this->contentTypeCondition
->setConfig('negate', 0);
foreach ($form_state
->getValues() as $key => $value) {
if ($key === 'vids') {
$vids = [];
foreach ($value as $vid) {
if (!empty($vid)) {
$vids[] = $vid;
}
}
// Vocabularies settings.
$condition
->set('vids', $vids);
}
else {
$condition
->set($key, $value);
}
}
// Path setting.
$condition
->set('path', $this->pathCondition
->getConfiguration());
// Content type settings.
$condition
->set('contentTypes', $this->contentTypeCondition
->getConfiguration());
$status = $condition
->save();
if ($status) {
$this
->messenger()
->addMessage($this
->t('Saved the %label Example.', [
'%label' => $condition
->label(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('The %label Example was not saved.', [
'%label' => $condition
->label(),
]), MessengerInterface::TYPE_ERROR);
}
// We need to invalidate all node pages by node_view tag
// to apply the changes make here.
\Drupal::service('cache_tags.invalidator')
->invalidateTags([
'node_view',
]);
$form_state
->setRedirect('entity.tooltip_taxonomy.config');
}
/**
* Helper function to check whether an Example configuration entity exists.
*/
public function exist($id) {
$entity = $this->entityTypeManager
->getStorage('filter_condition')
->getQuery()
->condition('cid', $id)
->execute();
return (bool) $entity;
}
/**
* Returns an array of existing field storages that can be added to node.
*
* @return array
* An array of existing field storages keyed by name.
*/
protected function getExistingFieldStorageOptions() {
$options = [];
// Load the field_storages and build the list of options.
$field_types = $this->fieldTypePluginManager
->getDefinitions();
$fields = $this->entityTypeManager
->getStorage('field_storage_config')
->loadByProperties([
'deleted' => FALSE,
// 'type' => 'entity_reference',.
'status' => 1,
]);
foreach ($fields as $field_name => $field_storage) {
// Do not show:
// - non-configurable field storages,
// - locked field storages,
// - non-text field,.
$field_type = $field_storage
->getType();
$field_key_name = str_replace('.', '-', $field_name);
if ($field_storage instanceof FieldStorageConfigInterface && $this->fieldTypeManager
->isContentField($field_storage
->getName()) && $this->fieldTypeManager
->isTextField($field_type) && !$field_storage
->isLocked()) {
$options[$field_key_name] = $this
->t('@type: @field', [
'@type' => $field_types[$field_type]['label'],
'@field' => $field_name,
]);
}
}
asort($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 being used by this form. | 7 |
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 | ||
FilterConditionForm:: |
protected | property | Drupal condition plugin for content type. | |
FilterConditionForm:: |
protected | property | The entity type bundle info. | |
FilterConditionForm:: |
protected | property |
The entity type manager. Overrides EntityForm:: |
|
FilterConditionForm:: |
protected | property | The field type manager. | |
FilterConditionForm:: |
protected | property | The field type plugin manager. | |
FilterConditionForm:: |
protected | property | Drupal Condition plugin for path. | |
FilterConditionForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FilterConditionForm:: |
public | function | Helper function to check whether an Example configuration entity exists. | |
FilterConditionForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
FilterConditionForm:: |
protected | function | Returns an array of existing field storages that can be added to node. | |
FilterConditionForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
FilterConditionForm:: |
public | function | Constructs an FilterConditionForm 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. |