You are here

public function SettingsForm::buildForm in Sophron 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 82

Class

SettingsForm
Main Sophron settings admin form.

Namespace

Drupal\sophron\Form

Code

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);
}