class LayoutParagraphsSectionsSettingsForm in Layout Paragraphs 2.0.x
Same name and namespace in other branches
- 1.0.x src/Form/LayoutParagraphsSectionsSettingsForm.php \Drupal\layout_paragraphs\Form\LayoutParagraphsSectionsSettingsForm
Class LayoutParagraphsSectionsSettingsForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\layout_paragraphs\Form\LayoutParagraphsSectionsSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of LayoutParagraphsSectionsSettingsForm
1 string reference to 'LayoutParagraphsSectionsSettingsForm'
File
- src/
Form/ LayoutParagraphsSectionsSettingsForm.php, line 17
Namespace
Drupal\layout_paragraphs\FormView source
class LayoutParagraphsSectionsSettingsForm extends ConfigFormBase {
/**
* The typed config service.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The Entity Type Manager service property.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Layouts Manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManager
*/
protected $layoutPluginManager;
/**
* SettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The typed config service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Core entity type manager service.
* @param \Drupal\Core\Layout\LayoutPluginManager $layout_plugin_manager
* Core layout plugin manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, LayoutPluginManager $layout_plugin_manager) {
parent::__construct($config_factory);
$this->typedConfigManager = $typedConfigManager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityTypeManager = $entity_type_manager;
$this->layoutPluginManager = $layout_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('config.typed'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.core.layout'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'layout_paragraphs_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'layout_paragraphs.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$paragraph_bundles = $this->entityTypeBundleInfo
->getBundleInfo('paragraph');
$form['description'] = [
'#type' => 'markup',
'#markup' => '<p>' . $this
->t('Choose at least one paragraph type to use as a Layout Section.') . '</p>',
];
$layout_options = $this->layoutPluginManager
->getLayoutOptions();
$paragraphs_type_storage = $this->entityTypeManager
->getStorage('paragraphs_type');
foreach ($paragraph_bundles as $name => $paragraph_bundle) {
/** @var \Drupal\paragraphs\Entity\ParagraphsType $paragraphs_type */
$paragraphs_type = $paragraphs_type_storage
->load($name);
$layout_paragraphs_behavior = $paragraphs_type
->getBehaviorPlugin('layout_paragraphs');
$layout_paragraphs_behavior_config = $layout_paragraphs_behavior
->getConfiguration();
$form[$name] = [
'#type' => 'fieldset',
'#title' => $paragraph_bundle['label'],
'#description' => $paragraphs_type
->getDescription(),
];
$form[$name][$name] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use as a Layout Section'),
'#default_value' => !empty($layout_paragraphs_behavior_config['enabled']),
];
$form[$name][$name . '_layouts'] = [
'#type' => 'select',
'#title' => $this
->t('Available Layouts for @label Paragraphs', [
'@label' => $paragraph_bundle['label'],
]),
'#options' => $layout_options,
'#multiple' => TRUE,
'#default_value' => array_keys($layout_paragraphs_behavior_config['available_layouts']),
'#size' => count($layout_options) < 8 ? count($layout_options) * 2 : 10,
'#states' => [
'visible' => [
':input[name="' . $name . '"]' => [
'checked' => TRUE,
],
],
],
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$paragraph_bundles = $this->entityTypeBundleInfo
->getBundleInfo('paragraph');
$paragraphs_type_storage = $this->entityTypeManager
->getStorage('paragraphs_type');
$layouts = $this->layoutPluginManager
->getSortedDefinitions();
foreach (array_keys($paragraph_bundles) as $name) {
/** @var \Drupal\paragraphs\Entity\ParagraphsType $paragraphs_type */
$paragraphs_type = $paragraphs_type_storage
->load($name);
$layout_paragraphs_behavior = $paragraphs_type
->getBehaviorPlugin('layout_paragraphs');
if ($form_state
->getValue($name)) {
$layout_paragraphs_behavior
->setConfiguration([
'enabled' => TRUE,
]);
$config = [
'enabled' => TRUE,
'available_layouts' => [],
];
foreach ($form_state
->getValue($name . '_layouts') as $layout_id) {
$config['available_layouts'][$layout_id] = $layouts[$layout_id]
->getLabel();
}
$layout_paragraphs_behavior
->setConfiguration($config);
}
else {
$layout_paragraphs_behavior
->setConfiguration([
'enabled' => FALSE,
]);
}
$paragraphs_type
->save();
}
$this
->messenger()
->addMessage($this
->t('The Layout Paragraphs settings have been saved.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
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. | |
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:: |
72 |
LayoutParagraphsSectionsSettingsForm:: |
protected | property | The entity type bundle info service. | |
LayoutParagraphsSectionsSettingsForm:: |
protected | property | The Entity Type Manager service property. | |
LayoutParagraphsSectionsSettingsForm:: |
protected | property | The Layouts Manager. | |
LayoutParagraphsSectionsSettingsForm:: |
protected | property | The typed config service. | |
LayoutParagraphsSectionsSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
LayoutParagraphsSectionsSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
LayoutParagraphsSectionsSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
LayoutParagraphsSectionsSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
LayoutParagraphsSectionsSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
LayoutParagraphsSectionsSettingsForm:: |
public | function |
SettingsForm constructor. Overrides ConfigFormBase:: |
|
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. |