You are here

class LayoutParagraphsSectionsSettingsForm in Layout Paragraphs 1.0.x

Same name and namespace in other branches
  1. 2.0.x src/Form/LayoutParagraphsSectionsSettingsForm.php \Drupal\layout_paragraphs\Form\LayoutParagraphsSectionsSettingsForm

Class LayoutParagraphsSectionsSettingsForm.

Hierarchy

Expanded class hierarchy of LayoutParagraphsSectionsSettingsForm

1 string reference to 'LayoutParagraphsSectionsSettingsForm'
layout_paragraphs.routing.yml in ./layout_paragraphs.routing.yml
layout_paragraphs.routing.yml

File

src/Form/LayoutParagraphsSectionsSettingsForm.php, line 17

Namespace

Drupal\layout_paragraphs\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LayoutParagraphsSectionsSettingsForm::$entityTypeBundleInfo protected property The entity type bundle info service.
LayoutParagraphsSectionsSettingsForm::$entityTypeManager protected property The Entity Type Manager service property.
LayoutParagraphsSectionsSettingsForm::$layoutPluginManager protected property The Layouts Manager.
LayoutParagraphsSectionsSettingsForm::$typedConfigManager protected property The typed config service.
LayoutParagraphsSectionsSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
LayoutParagraphsSectionsSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
LayoutParagraphsSectionsSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
LayoutParagraphsSectionsSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LayoutParagraphsSectionsSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
LayoutParagraphsSectionsSettingsForm::__construct public function SettingsForm constructor. Overrides ConfigFormBase::__construct
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.