You are here

class SettingsForm in Sophron 8

Main Sophron settings admin form.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
sophron.routing.yml in ./sophron.routing.yml
sophron.routing.yml

File

src/Form/SettingsForm.php, line 18

Namespace

Drupal\sophron\Form
View source
class SettingsForm extends ConfigFormBase {
  use SchemaCheckTrait;

  /**
   * The MIME map manager service.
   *
   * @var \Drupal\sophron\MimeMapManagerInterface
   */
  protected $mimeMapManager;

  /**
   * The typed config service.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfig;

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

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

  /**
   * Constructs a SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\sophron\MimeMapManagerInterface $mime_map_manager
   *   The MIME map manager service.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
   *   The typed config service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MimeMapManagerInterface $mime_map_manager, TypedConfigManagerInterface $typed_config) {
    parent::__construct($config_factory);
    $this->mimeMapManager = $mime_map_manager;
    $this->typedConfig = $typed_config;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('sophron.mime_map.manager'), $container
      ->get('config.typed'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('sophron.settings');

    // Vertical tabs.
    $form['tabs'] = [
      '#type' => 'vertical_tabs',
      '#tree' => FALSE,
    ];

    // Guessing.
    $guessing_status = $this->mimeMapManager
      ->requirements('runtime')['mime_type_guessing_sophron'];
    $form['guessing'] = [
      '#type' => 'details',
      '#title' => $guessing_status['title'],
      '#group' => 'tabs',
    ];
    $form['guessing']['info'] = [
      '#type' => 'item',
      '#title' => $guessing_status['value'],
      '#description' => $guessing_status['description'],
    ];

    // Mapping.
    $form['mapping'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Mapping'),
      '#description' => $this
        ->t("Manage additional MIME types and mapping issues."),
      '#group' => 'tabs',
    ];
    $options = [
      MimeMapManagerInterface::DRUPAL_MAP => $this
        ->t("<strong>Sophron map.</strong> Use this map for maximum compatibility with Drupal, still increasing the number of MIME types and file extensions identified."),
      MimeMapManagerInterface::DEFAULT_MAP => $this
        ->t("<strong>MimeMap default map.</strong> Use this map for maximum compatibility with Apache and Freedesktop projects, accepting differences versus current Drupal mappings."),
      MimeMapManagerInterface::CUSTOM_MAP => $this
        ->t("<strong>Custom map.</strong> Use this option to select a custom built mapping class."),
    ];
    $form['mapping']['map_option'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Map'),
      '#default_value' => $config
        ->get('map_option'),
      '#options' => $options,
      '#required' => TRUE,
      '#description' => $this
        ->t("Select the map to use."),
    ];
    $form['mapping']['map_class'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Class name'),
      '#description' => $this
        ->t('A fully qualified PHP class name. The map class must extend from \\FileEye\\MimeMap\\Map\\AbstractMap.'),
      '#default_value' => $config
        ->get('map_class'),
      '#states' => [
        'visible' => [
          ':radio[name="map_option"]' => [
            'value' => MimeMapManagerInterface::CUSTOM_MAP,
          ],
        ],
      ],
    ];
    $commands = $config
      ->get('map_commands');
    $form['mapping']['map_commands'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Mapping commands'),
      '#description' => $this
        ->t("The commands below alter the default MIME type mapping. More information in the module's README.md file."),
      '#description_display' => 'before',
      '#rows' => 5,
      '#default_value' => empty($commands) ? '' : Yaml::dump($commands, 1),
    ];

    // Mapping errors.
    if ($errors = $this->mimeMapManager
      ->getMappingErrors($this->mimeMapManager
      ->getMapClass())) {
      $form['mapping']['mapping_errors'] = [
        '#type' => 'details',
        '#collapsible' => TRUE,
        '#open' => TRUE,
        '#title' => $this
          ->t("Mapping errors"),
        '#description' => $this
          ->t("The list below shows the errors occurring in applying mapping commands to the map. Correct them to clean up the list."),
      ];
      $rows = [];
      foreach ($errors as $error) {
        $rows[] = [
          $error['method'],
          "'" . implode("', '", $error['args']) . "'",
          $error['type'],
          $error['message'],
        ];
      }
      $form['mapping']['mapping_errors']['table'] = [
        '#type' => 'table',
        '#id' => 'sophron-mapping-errors-table',
        '#header' => [
          [
            'data' => $this
              ->t('Method'),
          ],
          [
            'data' => $this
              ->t('Arguments'),
          ],
          [
            'data' => $this
              ->t('Error'),
          ],
          [
            'data' => $this
              ->t('Description'),
          ],
        ],
        '#rows' => $rows,
      ];
    }

    // Mapping gaps.
    if ($gaps = $this
      ->determineMapGaps($this->mimeMapManager
      ->getMapClass())) {
      $form['mapping']['gaps'] = [
        '#type' => 'details',
        '#collapsible' => TRUE,
        '#open' => FALSE,
        '#title' => $this
          ->t("Mapping gaps"),
        '#description' => $this
          ->t("The list below shows the gaps of the current map vs. Drupal's core MIME type mapping. Overcome the gaps by adding additional mapping commands."),
      ];
      $form['mapping']['gaps']['table'] = [
        '#type' => 'table',
        '#id' => 'sophron-mapping-gaps-table',
        '#header' => [
          [
            'data' => $this
              ->t('File extension'),
          ],
          [
            'data' => $this
              ->t('Drupal core MIME type'),
          ],
          [
            'data' => $this
              ->t('Gap'),
          ],
        ],
        '#rows' => $gaps,
      ];
    }

    // Mime types.
    $form['types'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('MIME types'),
      '#description' => $this
        ->t("List of MIME types and their file extensions."),
      '#group' => 'tabs',
    ];
    $rows = [];
    $i = 1;
    foreach ($this->mimeMapManager
      ->listTypes() as $type_string) {
      if ($type = $this->mimeMapManager
        ->getType($type_string)) {
        $rows[] = [
          $i++,
          $type_string,
          implode(', ', $type
            ->getExtensions()),
          $type
            ->getDescription(),
          implode(', ', $type
            ->getAliases()),
        ];
      }
    }
    $form['types']['table'] = [
      '#type' => 'table',
      '#id' => 'sophron-mime-types-table',
      '#header' => [
        [
          'data' => $this
            ->t('#'),
        ],
        [
          'data' => $this
            ->t('MIME Type'),
        ],
        [
          'data' => $this
            ->t('File extensions'),
        ],
        [
          'data' => $this
            ->t('Description'),
        ],
        [
          'data' => $this
            ->t('Aliases'),
        ],
      ],
      '#rows' => $rows,
    ];

    // File extensions.
    $form['extensions'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('File extensions'),
      '#description' => $this
        ->t("List of file extensions and their MIME types."),
      '#group' => 'tabs',
    ];
    $rows = [];
    $i = 1;
    foreach ($this->mimeMapManager
      ->listExtensions() as $extension_string) {
      if ($extension = $this->mimeMapManager
        ->getExtension($extension_string)) {
        $rows[] = [
          $i++,
          $extension_string,
          implode(', ', $extension
            ->getTypes()),
          $this->mimeMapManager
            ->getType($extension
            ->getDefaultType())
            ->getDescription(),
        ];
      }
    }
    $form['extensions']['table'] = [
      '#type' => 'table',
      '#id' => 'sophron-extensions-table',
      '#header' => [
        [
          'data' => $this
            ->t('#'),
        ],
        [
          'data' => $this
            ->t('File extension'),
        ],
        [
          'data' => $this
            ->t('MIME types'),
        ],
        [
          'data' => $this
            ->t('Description'),
        ],
      ],
      '#rows' => $rows,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Custom map class.
    if ($form_state
      ->getValue('map_option') == MimeMapManagerInterface::CUSTOM_MAP && !$this->mimeMapManager
      ->isMapClassValid($form_state
      ->getValue('map_class'))) {
      $form_state
        ->setErrorByName('map_class', $this
        ->t("The map class is invalid. Make sure the selected class is an extension of \\FileEye\\MimeMap\\Map\\AbstractMap."));
    }

    // Mapping commands.
    if ($form_state
      ->getValue('map_commands') !== '') {
      try {
        $map_commands = Yaml::parse($form_state
          ->getValue('map_commands'));
        $data = $this->configFactory
          ->get('sophron.settings')
          ->get();
        $data['map_commands'] = $map_commands;
        $schema_errors = $this
          ->checkConfigSchema($this->typedConfig, 'sophron.settings', $data);
        if (is_array($schema_errors)) {
          $fail_items = [];
          foreach ($schema_errors as $key => $value) {
            $matches = [];
            if (preg_match('/sophron\\.settings\\:map\\_commands\\.(\\d+)/', $key, $matches)) {
              $item = (int) $matches[1] + 1;
              $fail_items[$item] = $item;
            }
          }
          $form_state
            ->setErrorByName('map_commands', $this
            ->t("The items at line(s) @lines are wrongly typed. Make sure they follow the pattern '- [method, [arg1, ..., argN]]'.", [
            '@lines' => implode(', ', $fail_items),
          ]));
        }
      } catch (\Exception $e) {
        $form_state
          ->setErrorByName('map_commands', $this
          ->t("YAML syntax error: @error", [
          '@error' => $e
            ->getMessage(),
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->getEditable('sophron.settings');
    try {
      $config
        ->set('map_option', $form_state
        ->getValue('map_option'));
      $config
        ->set('map_class', $form_state
        ->getValue('map_class'));
      $commands = Yaml::parse($form_state
        ->getValue('map_commands'));
      $config
        ->set('map_commands', $commands ?: []);
      $config
        ->save();
    } catch (\Exception $e) {

      // Do nothing.
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Returns an array of gaps of current map vs Drupal's core mapping.
   *
   * @return array
   *   An array of simple arrays, each having a file extension, its Drupal MIME
   *   type guess, and a gap information.
   */
  protected function determineMapGaps() {
    $core_extended_guesser = new CoreExtensionMimeTypeGuesserExtended();
    $extensions = $core_extended_guesser
      ->listExtensions();
    sort($extensions);
    $rows = [];
    foreach ($extensions as $extension_string) {
      $drupal_mime_type = $core_extended_guesser
        ->guess('a.' . $extension_string);
      $extension = $this->mimeMapManager
        ->getExtension($extension_string);
      if ($extension) {
        try {
          $mimemap_mime_type = $extension
            ->getDefaultType();
        } catch (\Exception $e) {
          $mimemap_mime_type = '';
        }
      }
      $gap = '';
      if ($mimemap_mime_type === '') {
        $gap = $this
          ->t('No MIME type mapped to this file extension.');
      }
      elseif (mb_strtolower($drupal_mime_type) != mb_strtolower($mimemap_mime_type)) {
        $gap = $this
          ->t("File extension mapped to '@type' instead.", [
          '@type' => $mimemap_mime_type,
        ]);
      }
      if ($gap !== '') {
        $rows[] = [
          $extension_string,
          $drupal_mime_type,
          $gap,
        ];
      }
    }
    return $rows;
  }

}

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.
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.
SchemaCheckTrait::$configName protected property The configuration object name under test.
SchemaCheckTrait::$schema protected property The config schema wrapper object for the configuration object under test.
SchemaCheckTrait::checkConfigSchema public function Checks the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTrait::checkValue protected function Helper method to check data type.
SettingsForm::$mimeMapManager protected property The MIME map manager service.
SettingsForm::$typedConfig protected property The typed config service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::determineMapGaps protected function Returns an array of gaps of current map vs Drupal's core mapping.
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SettingsForm::__construct public function Constructs a SettingsForm object. Overrides ConfigFormBase::__construct
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.