Skip to content
Snippets Groups Projects
Commit 5ca17beb authored by Jay Huskins's avatar Jay Huskins Committed by Eric Bremner
Browse files

Resolve #3306673 "Module configuration"

parent f60e1378
No related branches found
No related tags found
1 merge request!3Resolve #3306673 "Module configuration"
block_id: 1
section_id: 1
layout_builder_ids.settings:
type: config_object
label: "Layout Builder IDs settings"
mapping:
block_id:
type: integer
label: "Whether to add an ID field to the block configuration form in layout builder"
section_id:
type: integer
label: "Whether to add an ID field to the section configuration form in layout builder"
layout_builder_ids.settings:
title: "Layout Builder IDs"
description: "Configure the Layout Builder IDs module."
route_name: layout_builder_ids.settings
parent: system.admin_config_ui
...@@ -9,9 +9,10 @@ ...@@ -9,9 +9,10 @@
* Implements template_preprocess_layout. * Implements template_preprocess_layout.
*/ */
function layout_builder_ids_preprocess_layout(&$variables) { function layout_builder_ids_preprocess_layout(&$variables) {
$config = \Drupal::config('layout_builder_ids.settings');
// If there is a layout builder id, set it in the attributes. // If there is a layout builder id, set it in the attributes.
if (isset($variables['content']['#settings']['layout_builder_id']) && $variables['content']['#settings']['layout_builder_id'] !== '') { if ($config->get('section_id') && isset($variables['content']['#settings']['layout_builder_id']) && $variables['content']['#settings']['layout_builder_id'] !== '') {
// Set the id attribute. // Set the id attribute.
$variables['attributes']['id'] = $variables['content']['#settings']['layout_builder_id']; $variables['attributes']['id'] = $variables['content']['#settings']['layout_builder_id'];
......
layout_builder_ids.settings:
path: "/admin/config/user-interface/layout-builder-ids"
defaults:
_form: '\Drupal\layout_builder_ids\Form\LayoutBuilderIdsSettingsForm'
_title: "Layout Builder IDs settings"
requirements:
_permission: "administer site configuration"
...@@ -38,6 +38,11 @@ class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface { ...@@ -38,6 +38,11 @@ class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface {
* The event. * The event.
*/ */
public static function alterForm(FormAlterEvent $event): void { public static function alterForm(FormAlterEvent $event): void {
// Ignore form alter according to module configuration.
$config = \Drupal::config('layout_builder_ids.settings');
if (!$config->get('block_id')) {
return;
}
// Get the form from the event. // Get the form from the event.
$form = &$event->getForm(); $form = &$event->getForm();
......
...@@ -38,6 +38,11 @@ class LayoutBuilderIdsConfigureSection implements EventSubscriberInterface { ...@@ -38,6 +38,11 @@ class LayoutBuilderIdsConfigureSection implements EventSubscriberInterface {
* The event. * The event.
*/ */
public static function alterForm(FormAlterEvent $event): void { public static function alterForm(FormAlterEvent $event): void {
// Ignore form alter according to module configuration.
$config = \Drupal::config('layout_builder_ids.settings');
if (!$config->get('section_id')) {
return;
}
// Get the form from the event. // Get the form from the event.
$form = &$event->getForm(); $form = &$event->getForm();
......
...@@ -33,6 +33,11 @@ class LayoutBuilderIdsRenderSubscriber implements EventSubscriberInterface { ...@@ -33,6 +33,11 @@ class LayoutBuilderIdsRenderSubscriber implements EventSubscriberInterface {
* The section component render event. * The section component render event.
*/ */
public static function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { public static function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
// Ignore block alter according to module configuration.
$config = \Drupal::config('layout_builder_ids.settings');
if (!$config->get('block_id')) {
return;
}
// The render array. // The render array.
$build = $event->getBuild(); $build = $event->getBuild();
......
<?php
namespace Drupal\layout_builder_ids\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure Layout Builder IDs settings for this site.
*/
class LayoutBuilderIdsSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'layout_builder_ids_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['layout_builder_ids.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('layout_builder_ids.settings');
$form['block_id'] = [
'#type' => 'checkbox',
'#title' => $this->t('Block ID field.'),
'#description' => $this->t('Add an ID field to <em>block</em> configuration form in Layout Builder.'),
'#default_value' => $config->get('block_id'),
];
$form['section_id'] = [
'#type' => 'checkbox',
'#title' => $this->t('Section ID field.'),
'#description' => $this->t('Add an ID field to <em>section</em> configuration form in Layout Builder.'),
'#default_value' => $config->get('section_id'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('layout_builder_ids.settings')
->set('block_id', $form_state->getValue('block_id'))
->set('section_id', $form_state->getValue('section_id'))
->save();
parent::submitForm($form, $form_state);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment