You are here

public function MediaBundle::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/MediaBundle.php, line 110

Class

MediaBundle
Adds customized sort priority by Media Bundle.

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('Media Bundle'),
      $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) {

    // TODO Maybe this can be extended for non Node types?
    if ($datasource
      ->getEntityTypeId() == 'media') {
      if ($bundles = $datasource
        ->getBundles()) {

        // Make a dummy array to add custom weight.
        foreach ($bundles as $bundle_id => $bundle_name) {
          $weight = $this->configuration['weight'];
          if (isset($this->configuration['sorttable']) && isset($this->configuration['sorttable'][$bundle_id]['weight'])) {
            $weight = $this->configuration['sorttable'][$bundle_id]['weight'];
          }
          $bundle_weight[$bundle_id]['bundle_id'] = $bundle_id;
          $bundle_weight[$bundle_id]['bundle_name'] = $bundle_name;
          $bundle_weight[$bundle_id]['weight'] = $weight;
        }

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

        // Loop over each bundle and create a form row.
        foreach ($bundle_weight as $bundle_id => $bundle) {
          $weight = $bundle['weight'];
          $bundle_name = $bundle['bundle_name'];

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

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

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

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