You are here

class StylesOverviewForm in Paragraphs Collection 8

Provides a form for revision overview page.

Hierarchy

Expanded class hierarchy of StylesOverviewForm

File

src/Form/StylesOverviewForm.php, line 13

Namespace

Drupal\paragraphs_collection\Form
View source
class StylesOverviewForm extends ConfigFormBase {

  /**
   * The discovery service for style files.
   *
   * @var \Drupal\paragraphs_collection\StyleDiscoveryInterface
   */
  protected $styleDiscovery;

  /**
   * Wrapper object for simple configuration from diff.settings.yml.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Constructs a RevisionOverviewForm object.
   *
   * @param \Drupal\paragraphs_collection\StyleDiscoveryInterface $style_discovery
   *   The discovery service for style files.
   */
  public function __construct(StyleDiscoveryInterface $style_discovery) {
    $this->config = $this
      ->config('paragraphs_collection.settings');
    $this->styleDiscovery = $style_discovery;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('paragraphs_collection.style_discovery'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'paragraphs_collection.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'paragraphs_collection_styles_overview_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $grouped_styles = NULL) {
    $group_options = $this->styleDiscovery
      ->getStyleGroupsLabel();
    asort($group_options);
    $empty_option = [
      '' => '- All -',
    ];
    $filters = [
      '#type' => 'fieldset',
      '#attributes' => [
        'class' => [
          'table-filter',
          'js-show',
          'form--inline',
        ],
        'data-table' => '.paragraphs-collection-overview-table',
      ],
      '#title' => $this
        ->t('Filter'),
    ];
    $filters['group'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Group'),
      '#options' => $empty_option + $group_options,
      '#attributes' => [
        'class' => [
          'table-filter-group-select',
        ],
      ],
    ];
    $filters['text'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Style label or ID'),
      '#size' => 40,
      '#attributes' => [
        'class' => [
          'table-filter-text',
        ],
        'autocomplete' => 'off',
        'title' => $this
          ->t('Enter a part of the style label or ID to filter by.'),
      ],
    ];
    $header = [
      'enabled' => $this
        ->t('Style'),
      'details' => $this
        ->t('Details'),
      'use' => $this
        ->t('Used in'),
    ];
    $styles = $this->styleDiscovery
      ->getStyles();
    $enabled_styles = $this->config
      ->get('enabled_styles');
    $rows = [];
    foreach ($grouped_styles as $style_id => $value) {
      $style = $styles[$style_id];
      $paragraphs_type_link_list = [];
      foreach ($value as $paragraphs_type_id => $paragraphs_type) {

        /** @var \Drupal\paragraphs\Entity\ParagraphsType $paragraphs_type */
        if ($paragraphs_type_link_list != []) {
          $paragraphs_type_link_list[] = [
            '#plain_text' => ', ',
          ];
        }
        $paragraphs_type_link_list[] = [
          '#type' => 'link',
          '#title' => $paragraphs_type
            ->label(),
          '#url' => $paragraphs_type
            ->toUrl(),
          '#attributes' => [
            'class' => [
              'table-filter-paragraphs-type-source',
            ],
          ],
        ];
      }
      $group_list = [];
      foreach ($style['groups'] as $group) {
        if ($group_list != []) {
          $group_list[] = [
            '#plain_text' => ', ',
          ];
        }
        $group_list[] = [
          '#type' => 'container',
          '#plain_text' => $group,
          '#attributes' => [
            'class' => [
              'table-filter-group-source',
            ],
          ],
        ];
      }
      $rows[$style_id]['enabled'] = array(
        '#type' => 'checkbox',
        '#title' => $style['title'],
        '#title_display' => 'after',
        '#default_value' => in_array($style_id, $enabled_styles) ?: FALSE,
      );
      $rows[$style_id]['details'] = [
        '#type' => 'details',
        '#title' => !empty($style['description']) ? $style['description'] : $this
          ->t('Description not available.'),
        '#open' => FALSE,
        '#attributes' => [
          'class' => [
            'overview-details',
          ],
        ],
      ];
      $rows[$style_id]['details']['id'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('ID'),
        '#prefix' => '<span class="container-inline">',
        '#suffix' => '</span>',
        'item' => [
          '#type' => 'container',
          '#plain_text' => $style_id,
          '#attributes' => [
            'class' => [
              'table-filter-text-source',
            ],
          ],
        ],
      ];
      $rows[$style_id]['details']['groups'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Groups'),
        'item' => $group_list,
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
      ];
      $rows[$style_id]['use'] = $paragraphs_type_link_list;
    }
    $table = [
      '#type' => 'table',
      '#header' => $header,
      '#sticky' => TRUE,
      '#attributes' => [
        'class' => [
          'paragraphs-collection-overview-table',
        ],
      ],
    ];
    $table += $rows;
    $form['filters'] = $filters;
    $form['styles'] = $table;
    $form['#attached']['library'] = [
      'paragraphs_collection/overview',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $enabled_styles = array_keys(array_filter((array) $form_state
      ->getValue('styles'), function ($value) {
      return !empty($value['enabled']);
    }));
    $this->config
      ->set('enabled_styles', $enabled_styles);
    $this->config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
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. Overrides UrlGeneratorTrait::redirect
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 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
StylesOverviewForm::$config protected property Wrapper object for simple configuration from diff.settings.yml.
StylesOverviewForm::$styleDiscovery protected property The discovery service for style files.
StylesOverviewForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
StylesOverviewForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
StylesOverviewForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
StylesOverviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
StylesOverviewForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
StylesOverviewForm::__construct public function Constructs a RevisionOverviewForm object. Overrides ConfigFormBase::__construct
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.