class SmartTitleConfigForm in Smart Title 8
SmartTitleConfigForm.
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\smart_title_ui\Form\SmartTitleConfigForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SmartTitleConfigForm
1 string reference to 'SmartTitleConfigForm'
- smart_title_ui.routing.yml in modules/
smart_title_ui/ smart_title_ui.routing.yml - modules/smart_title_ui/smart_title_ui.routing.yml
File
- modules/
smart_title_ui/ src/ Form/ SmartTitleConfigForm.php, line 21
Namespace
Drupal\smart_title_ui\FormView source
class SmartTitleConfigForm extends ConfigFormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
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 new UpdateSettingsForm.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @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_bunde_info
* The entity type bundle info service.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bunde_info) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeBundleInfo = $entity_type_bunde_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'smart_title.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'smart_title_config_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('smart_title.settings');
$entity_type_definitions = $this->entityTypeManager
->getDefinitions();
// Collecting content entity types which have canonical link template.
$content_entity_type_filter = function (EntityTypeInterface $entity_type_definition) {
return $entity_type_definition instanceof ContentEntityTypeInterface && $entity_type_definition
->entityClassImplements(FieldableEntityInterface::class) && $entity_type_definition
->get('field_ui_base_route');
};
$valid_content_entity_types = array_filter($entity_type_definitions, $content_entity_type_filter);
$entity_bundles = [];
foreach (array_keys($valid_content_entity_types) as $entity_type_id) {
$label_key = $valid_content_entity_types[$entity_type_id]
->getKey('label');
if ($label_key) {
$base_field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type_id);
if (!$base_field_definitions[$label_key]
->isDisplayConfigurable('view')) {
$entity_bundles[$entity_type_id]['label'] = $valid_content_entity_types[$entity_type_id]
->getLabel();
$entity_bundles[$entity_type_id]['bundles'] = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
}
}
}
$defaults = $config
->get('smart_title') ?: [];
foreach ($entity_bundles as $type => $definitions) {
$options = [];
$default = [];
foreach ($definitions['bundles'] as $key => $info) {
$options["{$type}:{$key}"] = $info['label'];
if (in_array("{$type}:{$key}", $defaults)) {
$default["{$type}:{$key}"] = "{$type}:{$key}";
}
}
$form[$type . '_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Smart Title for @entity-type', [
'@entity-type' => $definitions['label'],
]),
'#options' => $options,
'#default_value' => $default,
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->getValues();
$smart_title_bundles_setting = $smart_title_bundles = [];
foreach ($values as $key => $bundle_values) {
if (strpos($key, '_bundles')) {
foreach ($bundle_values as $bundle_key => $bundle_value) {
if ($bundle_value) {
$smart_title_bundles_setting[] = $bundle_key;
}
$smart_title_bundles[] = $bundle_key;
}
}
}
// Updating entity view displays:
// Remove smart title where it's not available anymore.
$evd_storage = $this->entityTypeManager
->getStorage('entity_view_display');
$evds = $evd_storage
->loadMultiple();
$not_smart_title_capable_bundles = array_diff($smart_title_bundles, $smart_title_bundles_setting);
foreach ($evds as $evd_id => $evd) {
assert($evd instanceof EntityViewDisplayInterface);
list($target_entity_type_id, $target_bundle) = explode('.', $evd_id);
if (in_array("{$target_entity_type_id}:{$target_bundle}", $not_smart_title_capable_bundles)) {
$evd
->unsetThirdPartySetting('smart_title', 'enabled')
->unsetThirdPartySetting('smart_title', 'settings')
->save();
}
}
$this
->config('smart_title.settings')
->set('smart_title', $smart_title_bundles_setting)
->save();
Cache::invalidateTags([
'entity_field_info',
]);
}
}
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. | |
SmartTitleConfigForm:: |
protected | property | The entity field manager. | |
SmartTitleConfigForm:: |
protected | property | The entity type bundle info service. | |
SmartTitleConfigForm:: |
protected | property | The entity type manager. | |
SmartTitleConfigForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SmartTitleConfigForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SmartTitleConfigForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SmartTitleConfigForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SmartTitleConfigForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SmartTitleConfigForm:: |
public | function |
Constructs a new UpdateSettingsForm. 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. |