class ScannerAdminForm in Search and Replace Scanner 8
Form for configuring the default scanner settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\scanner\Form\ScannerAdminForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ScannerAdminForm
1 string reference to 'ScannerAdminForm'
File
- src/
Form/ ScannerAdminForm.php, line 19
Namespace
Drupal\scanner\FormView source
class ScannerAdminForm extends ConfigFormBase {
/**
* The scanner plugin manager.
*
* @var \Drupal\scanner\Plugin\ScannerPluginManager
*/
protected $scannerPluginManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Holds the entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity_type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Constructs a ScannerAdminForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\scanner\Plugin\ScannerPluginManager $scanner_plugin_manager
* The scanner plugin manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity bundle info service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ScannerPluginManager $scanner_plugin_manager, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
parent::__construct($config_factory);
$this->scannerPluginManager = $scanner_plugin_manager;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.scanner'), $container
->get('language_manager'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'scanner_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'scanner.admin_settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('scanner.admin_settings');
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Default options'),
];
$form['settings']['scanner_mode'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default: Case sensitive search mode'),
'#default_value' => $config
->get('scanner_mode'),
];
$form['settings']['scanner_wholeword'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default: Match whole word'),
'#default_value' => $config
->get('scanner_wholeword'),
];
$form['settings']['scanner_regex'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default: Regular expression search'),
'#default_value' => $config
->get('scanner_regex'),
];
$form['settings']['scanner_published'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default: Search published nodes only'),
'#default_value' => $config
->get('scanner_published'),
];
$form['settings']['scanner_pathauto'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default: Maintain custom aliases'),
'#default_value' => $config
->get('scanner_pathauto'),
];
$langs = $this
->getLanguages();
$form['settings']['scanner_language'] = [
'#type' => 'select',
'#title' => $this
->t('Default: Content language'),
'#options' => $langs,
'#default_value' => $config
->get('scanner_language'),
];
$available_entity_types = $this
->getAvailableEntityTypes();
$form['enabled_content_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enabled entity types'),
'#options' => $available_entity_types,
'#default_value' => $config
->get('enabled_content_types'),
'#description' => $this
->t('Third party plugins can be written to add more options here.'),
'#ajax' => [
'callback' => [
$this,
'getFieldsCallback',
],
'event' => 'change',
'wrapper' => 'content-type-fields',
],
];
$enabled_entity_types = $form_state
->getValue('enabled_content_types');
if ($enabled_entity_types === NULL) {
$enabled_entity_types = $this
->config('scanner.admin_settings')
->get('enabled_content_types');
}
$fields = $this
->getEntityTypeFields($available_entity_types, $enabled_entity_types);
$form['fields_of_selected_content_type'] = [
'#title' => $this
->t('Enabled fields'),
'#type' => 'checkboxes',
'#options' => $fields,
'#default_value' => $config
->get('fields_of_selected_content_type'),
'#prefix' => '<div id="content-type-fields">',
'#suffix' => '</div>',
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('scanner.admin_settings')
->set('scanner_mode', $form_state
->getValue('scanner_mode'))
->set('scanner_regex', $form_state
->getValue('scanner_regex'))
->set('scanner_wholeword', $form_state
->getValue('scanner_wholeword'))
->set('scanner_published', $form_state
->getValue('scanner_published'))
->set('scanner_pathauto', $form_state
->getValue('scanner_pathauto'))
->set('scanner_language', $form_state
->getValue('scanner_language'))
->set('enabled_content_types', array_filter($form_state
->getValue('enabled_content_types')))
->set('fields_of_selected_content_type', array_filter($form_state
->getValue('fields_of_selected_content_type')))
->save();
parent::submitForm($form, $form_state);
}
/**
* AJAX callback for fetching the entity type fields.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* List of entity type fields.
*/
public function getFieldsCallback(array $form, FormStateInterface $form_state) {
return $form['fields_of_selected_content_type'];
}
/**
* Gets a list of available entity types as input options.
*
* @return array
* An array containing the entity type options.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
protected function getAvailableEntityTypes() {
$options = [];
// Iterate over the available plugins to get their 'types'.
foreach ($this->scannerPluginManager
->getDefinitions() as $definition) {
$entity_type_id = $definition['type'];
try {
$entity_type = $this->entityTypeManager
->getStorage($entity_type_id)
->getEntityType();
} catch (PluginNotFoundException $ignored) {
// Non-existent entity was provided. Ignore it.
continue;
}
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle_id => $bundle) {
$options["{$entity_type_id}:{$bundle_id}"] = $this
->t('@entity_type » @bundle', [
'@entity_type' => $entity_type
->getLabel(),
'@bundle' => $bundle['label'],
]);
}
}
return $options;
}
/**
* Gets a list of entity fields as input options.
*
* @param array $available_entity_types
* List of available entity types.
* @param array $entity_types
* The entity types, with their relevant bundles.
*
* @return array
* An array containing the fields of the entity types.
*/
protected function getEntityTypeFields(array $available_entity_types, array $entity_types) {
$options = [];
// Iterate through each of the selected entity types and get their fields.
foreach ($entity_types as $key => $value) {
if (empty($value) || !isset($available_entity_types[$key])) {
// Ignore the entity type if it's unticked
// or the entity type no longer exists.
continue;
}
list($entity_type, $bundle) = explode(':', $key);
// Get the fields for the given entity type and bundle.
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
foreach ($field_definitions as $field_name => $field_definition) {
$allowed_field_type = [
// TODO:codebymikey:20200210 why no string_long?
'string',
'text_with_summary',
'text',
'text_long',
];
// We are only interested in certain field types.
if (in_array($field_definition
->getType(), $allowed_field_type, TRUE)) {
// Skip fields starting with "parent_" (Paragraphs internal fields).
if (strpos($field_name, 'parent_') === 0) {
continue;
}
$name_with_type = "{$entity_type}:{$bundle}:{$field_name}";
$options[$name_with_type] = $this
->t('@entity_bundle » @field_label <small><strong>(@field_name)</strong></small>', [
'@entity_bundle' => $available_entity_types[$key],
'@field_label' => $field_definition
->getLabel(),
'@field_name' => $field_name,
]);
}
}
}
return $options;
}
/**
* Gets a list of languages as input options.
*
* @return array
* An array containing the list of enabled languages.
*/
protected function getLanguages() {
$languages = $this->languageManager
->getLanguages();
$items = [
'all' => $this
->t('All'),
];
foreach ($languages as $language) {
$items[$language
->getId()] = $language
->getName();
}
return $items;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
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. | |
ScannerAdminForm:: |
protected | property | The entity field manager. | |
ScannerAdminForm:: |
protected | property | The entity_type bundle info service. | |
ScannerAdminForm:: |
protected | property | Holds the entity type manager. | |
ScannerAdminForm:: |
protected | property | The language manager. | |
ScannerAdminForm:: |
protected | property | The scanner plugin manager. | |
ScannerAdminForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ScannerAdminForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ScannerAdminForm:: |
protected | function | Gets a list of available entity types as input options. | |
ScannerAdminForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ScannerAdminForm:: |
protected | function | Gets a list of entity fields as input options. | |
ScannerAdminForm:: |
public | function | AJAX callback for fetching the entity type fields. | |
ScannerAdminForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ScannerAdminForm:: |
protected | function | Gets a list of languages as input options. | |
ScannerAdminForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ScannerAdminForm:: |
public | function |
Constructs a ScannerAdminForm object. Overrides ConfigFormBase:: |
|
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. |