You are here

class OverviewForm in Markdown 8.2

Hierarchy

Expanded class hierarchy of OverviewForm

1 string reference to 'OverviewForm'
markdown.routing.yml in ./markdown.routing.yml
markdown.routing.yml

File

src/Form/OverviewForm.php, line 17

Namespace

Drupal\markdown\Form
View source
class OverviewForm extends ConfigFormBase {

  /**
   * The Cache Tags Invalidator service.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The Markdown Extension Plugin Manager service.
   *
   * @var \Drupal\markdown\PluginManager\ExtensionManagerInterface
   */
  protected $extensionManager;

  /**
   * The Markdown service.
   *
   * @var \Drupal\markdown\MarkdownInterface
   */
  protected $markdown;

  /**
   * The Markdown Parser Plugin Manager service.
   *
   * @var \Drupal\markdown\PluginManager\ParserManagerInterface
   */
  protected $parserManager;

  /**
   * OverviewForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The Config Factory service.
   * @param CacheTagsInvalidatorInterface $cacheTagsInvalidator
   *   The Cache Tags Invalidator service.
   * @param \Drupal\markdown\MarkdownInterface $markdown
   *   The Markdown service.
   * @param \Drupal\markdown\PluginManager\ParserManagerInterface $parserManager
   *   The Markdown Parser Plugin Manager service.
   * @param \Drupal\markdown\PluginManager\ExtensionManagerInterface $extensionManager
   *   The Markdown Extension Plugin Manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, CacheTagsInvalidatorInterface $cacheTagsInvalidator, MarkdownInterface $markdown, ParserManagerInterface $parserManager, ExtensionManagerInterface $extensionManager) {
    parent::__construct($config_factory);
    $this->cacheTagsInvalidator = $cacheTagsInvalidator;
    $this->extensionManager = $extensionManager;
    $this->markdown = $markdown;
    $this->parserManager = $parserManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('cache_tags.invalidator'), $container
      ->get('markdown'), $container
      ->get('plugin.manager.markdown.parser'), $container
      ->get('plugin.manager.markdown.extension'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    $configNames = [
      'markdown.settings',
    ];
    foreach (array_keys($this->parserManager
      ->installedDefinitions()) as $name) {
      $configNames[] = "markdown.parser.{$name}";
    }
    return $configNames;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $defaultParser = $this->parserManager
      ->getDefaultParser();
    $form['#attached']['library'][] = 'markdown/admin';
    $form['weights'] = [
      '#tree' => TRUE,
    ];
    $form['enabled'] = [
      '#type' => 'details',
      '#description' => $this
        ->t('The default parser will be used in the event a parser was requested but not explicitly specified (i.e. Twig template). Ordering parsers will return them in a specific order when multiple parsers are requested (i.e. display benchmarking/parsing differences).'),
      '#description_display' => 'after',
    ];
    $form['enabled']['table'] = [
      '#type' => 'table',
      '#theme' => 'table__markdown_enabled_parsers',
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'self',
          'group' => 'parser-weight',
        ],
      ],
      '#attributes' => [
        'class' => [
          'markdown-enabled-parsers',
        ],
      ],
      '#header' => [
        'parser' => $this
          ->t('Parser'),
        'status' => $this
          ->t('Status'),
        'weight' => $this
          ->t('Weight'),
        'ops' => $this
          ->t('Operations'),
      ],
      '#rows' => [],
    ];
    $enabledParsers =& $form['enabled']['table']['#rows'];
    $form['disabled'] = [
      '#type' => 'details',
    ];
    $form['disabled']['table'] = [
      '#type' => 'table',
      '#theme' => 'table__markdown_disabled_parsers',
      '#header' => [
        'parser' => $this
          ->t('Parser'),
        'status' => $this
          ->t('Status'),
        'ops' => $this
          ->t('Operations'),
      ],
      '#attributes' => [
        'class' => [
          'markdown-disabled-parsers',
        ],
      ],
      '#rows' => [],
    ];
    $disabledParsers =& $form['disabled']['table']['#rows'];
    $form['unavailable'] = [
      '#type' => 'details',
    ];
    $form['unavailable']['table'] = [
      '#type' => 'table',
      '#theme' => 'table__markdown_unavailable_parsers',
      '#header' => [
        'parser' => $this
          ->t('Parser'),
        'status' => $this
          ->t('Status'),
      ],
      '#attributes' => [
        'class' => [
          'markdown-unavailable-parsers',
        ],
      ],
      '#rows' => [],
    ];
    $unavailableParsers =& $form['unavailable']['table']['#rows'];
    $configurations = [];
    foreach (array_keys($this->parserManager
      ->getDefinitions(FALSE)) as $parser_id) {
      $configurations[$parser_id] = $this
        ->config("markdown.parser.{$parser_id}")
        ->get() ?: [];
    }

    // Iterate over the parsers.
    foreach ($this->parserManager
      ->all($configurations) as $name => $parser) {
      $isDefault = $defaultParser
        ->getPluginId() === $name;
      $installed = FALSE;
      $enabled = $parser
        ->isEnabled();
      if ($installedLibrary = $parser
        ->getInstalledLibrary()) {
        $installed = TRUE;
        if ($enabled) {
          $table =& $enabledParsers;
        }
        else {
          $table =& $disabledParsers;
        }
        $library = $installedLibrary;
      }
      elseif ($preferredLibrary = $parser
        ->getPreferredLibrary()) {
        $table =& $unavailableParsers;
        $library = $preferredLibrary;
      }
      else {
        continue;
      }
      $rowClasses = [];
      $label = $parser
        ->getLabel(FALSE);
      $link = $parser
        ->getLink($label);
      $row = [];
      $row['parser'] = [
        'class' => [
          'parser',
        ],
        'data' => [
          '#type' => 'item',
          '#title' => $parser
            ->getLink($isDefault ? new FormattableMarkup('@link (default)', [
            '@link' => $link,
          ]) : $label),
          '#description' => $parser
            ->getDescription(),
          '#description_display' => 'after',
        ],
      ];
      $row['status'] = [
        'class' => [
          'status',
        ],
        'data' => [
          '#theme' => 'installable_library',
          '#plugin' => $parser,
          '#library' => $library,
        ],
      ];
      $ops = [];
      $dialogOptions = [
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 700,
          ]),
        ],
      ];
      $options = [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ];
      if ($installed && $enabled) {
        $ops['edit'] = [
          'title' => $this
            ->t('Edit'),
          'url' => Url::fromRoute('markdown.parser.edit', [
            'parser' => $parser,
          ], $options),
        ];
        $ops['disable'] = [
          'title' => $this
            ->t('Disable'),
          'url' => Url::fromRoute('markdown.parser.confirm_operation', [
            'parser' => $parser,
            'operation' => 'disable',
          ], $dialogOptions),
        ];
        if (!$isDefault) {
          $ops['default'] = [
            'title' => $this
              ->t('Set as default'),
            'url' => Url::fromRoute('markdown.parser.confirm_operation', [
              'parser' => $parser,
              'operation' => 'default',
            ], $dialogOptions),
          ];
        }
        $rowClasses[] = 'draggable';
        $form['weights'][$name] = [
          '#type' => 'number',
          '#title' => $this
            ->t('Weight for @title', [
            '@title' => $label,
          ]),
          '#title_display' => 'invisible',
          '#size' => 6,
          '#default_value' => $parser
            ->getWeight(),
          '#attributes' => [
            'class' => [
              'parser-weight',
            ],
          ],
        ];
        $row['weight'] = [
          'class' => [
            'weight',
          ],
          'data' => &$form['weights'][$name],
        ];
        $form['weights'][$name]['#printed'] = TRUE;
        $table[$name] = [
          'class' => [
            'draggable',
          ],
          'data' => $row,
        ];
      }
      elseif ($installed && !$enabled) {
        $ops['enable'] = [
          'title' => $this
            ->t('Enable'),
          'url' => Url::fromRoute('markdown.parser.confirm_operation', [
            'parser' => $parser,
            'operation' => 'enable',
          ], $dialogOptions),
        ];
      }
      if ($ops) {
        $row['ops'] = [
          'class' => [
            'ops',
          ],
          'data' => [
            '#type' => 'dropbutton',
            '#dropbutton_type' => 'small',
            '#attached' => [
              'library' => [
                'core/drupal.dialog.ajax',
              ],
            ],
            '#links' => $ops,
          ],
        ];
      }
      $table[$name] = [
        'class' => $rowClasses,
        'data' => $row,
      ];
    }
    $form['enabled']['#title'] = $this
      ->t('Enabled Parsers (@count)', [
      '@count' => count($enabledParsers),
    ]);
    $form['disabled']['#title'] = $this
      ->t('Disabled Parsers (@count)', [
      '@count' => count($disabledParsers),
    ]);
    $form['unavailable']['#title'] = $this
      ->t('Unavailable Parsers (@count)', [
      '@count' => count($unavailableParsers),
    ]);
    $form['enabled']['#access'] = !!$enabledParsers;
    $form['disabled']['#access'] = !!$disabledParsers;
    $form['unavailable']['#access'] = !!$unavailableParsers;
    $form['enabled']['#open'] = FALSE;
    $form['disabled']['#open'] = FALSE;
    $form['unavailable']['#open'] = FALSE;
    if ($enabledParsers) {
      $form['enabled']['#open'] = TRUE;
    }
    elseif ($disabledParsers) {
      $form['disabled']['#open'] = TRUE;
    }
    elseif ($unavailableParsers) {
      $form['unavailable']['#open'] = TRUE;
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $invalidateCacheTags = [];
    if ($weights = $form_state
      ->getValue('weights')) {
      asort($weights, SORT_NUMERIC);

      // Reset weights so they start at 0.
      $i = 0;
      foreach (array_keys($weights) as $name) {
        $configName = "markdown.parser.{$name}";
        $config = $this
          ->config($configName);
        if ($config
          ->get('weight') !== $i) {
          $config
            ->set('weight', $i)
            ->save();
          $invalidateCacheTags[] = $configName;
        }
        $i++;
      }
    }
    $this->parserManager
      ->clearCachedDefinitions();
    if ($invalidateCacheTags) {
      $this->cacheTagsInvalidator
        ->invalidateTags($invalidateCacheTags);
    }
    parent::submitForm($form, $form_state);
  }

}

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.
OverviewForm::$cacheTagsInvalidator protected property The Cache Tags Invalidator service.
OverviewForm::$extensionManager protected property The Markdown Extension Plugin Manager service.
OverviewForm::$markdown protected property The Markdown service.
OverviewForm::$parserManager protected property The Markdown Parser Plugin Manager service.
OverviewForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
OverviewForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
OverviewForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
OverviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
OverviewForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
OverviewForm::__construct public function OverviewForm constructor. Overrides ConfigFormBase::__construct
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.
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.