You are here

public function FileMime::buildConfigurationForm in Search API Sort Priority 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/search_api/processor/FileMime.php, line 110

Class

FileMime
Adds customized sort priority by File mime.

Namespace

Drupal\search_api_sort_priority\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form['sorttable'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('File mime'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'sorttable-order-weight',
      ],
    ],
  ];

  // Get a list of available bundle_types defined on this index.
  $datasources = $this->index
    ->getDatasources();
  foreach ($datasources as $datasource) {
    if ($datasource
      ->getEntityTypeId() == 'file') {
      $mimeTypes = $this
        ->getAvailableMimes();
      if ($mimeTypes) {

        // Make a dummy array to add custom weight.
        foreach ($mimeTypes as $mimeType) {
          $weight = $this->configuration['weight'];
          if (isset($this->configuration['sorttable']) && isset($this->configuration['sorttable'][$mimeType]['weight'])) {
            $weight = $this->configuration['sorttable'][$mimeType]['weight'];
          }
          $mime_weight[$mimeType]['mime_type'] = $mimeType;
          $mime_weight[$mimeType]['weight'] = $weight;
        }

        // Sort weights.
        uasort($mime_weight, [
          'Drupal\\Component\\Utility\\SortArray',
          'sortByWeightElement',
        ]);

        // Loop over each mime and create a form row.
        foreach ($mime_weight as $mime) {
          $weight = $mime['weight'];
          $mimeType = $mime['mime_type'];

          // Add form with weights
          // Mark the table row as draggable.
          $form['sorttable'][$mimeType]['#attributes']['class'][] = 'draggable';

          // Sort the table row according to its existing/configured weight.
          $form['sorttable'][$mimeType]['#weight'] = $weight;

          // Table columns containing raw markup.
          $form['sorttable'][$mimeType]['label']['#plain_text'] = $mimeType;

          // Weight column element.
          $form['sorttable'][$mimeType]['weight'] = [
            '#type' => 'weight',
            '#title' => t('Weight for @title', [
              '@title' => $mimeType,
            ]),
            '#title_display' => 'invisible',
            '#default_value' => $weight,
            // Classify the weight element for #tabledrag.
            '#attributes' => [
              'class' => [
                'sorttable-order-weight',
              ],
            ],
          ];
        }
      }
    }
  }
  return $form;
}