You are here

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

Class

Role
Adds customized sort priority by Role.

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('Role'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'sorttable-order-weight',
      ],
    ],
  ];
  $master_roles = user_roles();
  $roles = array_map(function (RoleInterface $role) {
    return Html::escape($role
      ->label());
  }, $master_roles);

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

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

  // Loop over each role and create a form row.
  foreach ($role_weight as $role_id => $role) {
    $weight = $role['weight'];
    $role_name = $role['bundle_name'];

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

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

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

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