You are here

public function FacetListBuilder::buildForm in Facets 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 DraggableListBuilder::buildForm

File

src/FacetListBuilder.php, line 223

Class

FacetListBuilder
Builds a listing of facet entities.

Namespace

Drupal\facets

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $groups = $this
    ->loadGroups();
  $form['facets'] = [
    '#type' => 'table',
    '#header' => $this
      ->buildHeader(),
    '#empty' => $groups['lone_facets'] ? '' : $this
      ->t('There are no facet sources or facets defined.'),
    '#attributes' => [
      'class' => [
        'facets-groups-list',
      ],
    ],
  ];

  // When no facet sources are found, we should show a message that you can't
  // add facets yet.
  if (empty($groups['facet_source_groups'])) {
    return [
      '#markup' => $this
        ->t('You currently have no facet sources defined. You should start by adding a facet source before creating facets.<br />
           An example of a facet source is a view based on Search API or a Search API page.
           Other modules can also implement a facet source by providing a plugin that implements the FacetSourcePluginInterface.'),
    ];
  }
  $form['#attached']['library'][] = 'facets/drupal.facets.admin_css';
  foreach ($groups['facet_source_groups'] as $facet_source_group) {
    $subgroup_class = Html::cleanCssIdentifier('facets-weight-' . $facet_source_group['facet_source']['id']);
    $delta = round(count($facet_source_group['facets']) / 2);
    $form['facets']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'weight',
      'subgroup' => $subgroup_class,
    ];
    $form['facets'][$facet_source_group['facet_source']['id']] = $this
      ->buildFacetSourceRow($facet_source_group['facet_source']);
    foreach ($facet_source_group['facets'] as $facet) {
      if ($facet instanceof FacetInterface) {
        $form['facets'][$facet
          ->id()] = $this
          ->buildRow($facet);
        $form['facets'][$facet
          ->id()]['weight']['#attributes']['class'][] = $subgroup_class;
        $form['facets'][$facet
          ->id()]['weight']['#delta'] = $delta;
      }
      elseif ($facet instanceof FacetsSummaryInterface) {
        $form['facets'][$facet
          ->id()] = $this
          ->buildFacetSummaryRow($facet);
      }
    }
  }

  // Output the list of facets without a facet source separately.
  if (!empty($groups['lone_facets'])) {
    $subgroup_class = 'facets-weight-lone-facets';
    $form['facets']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'weight',
      'subgroup' => $subgroup_class,
    ];
    $form['facets']['lone_facets'] = [
      'type' => [
        '#theme_wrappers' => [
          'container' => [
            '#attributes' => [
              'class' => 'facets-type',
            ],
          ],
        ],
        '#type' => 'markup',
        '#markup' => '<h3>' . $this
          ->t('Facets not currently associated with any facet source') . '</h3>',
      ],
      '#wrapper_attributes' => [
        'colspan' => 4,
      ],
    ];

    /** @var \Drupal\facets\FacetInterface $facet */
    foreach ($groups['lone_facets'] as $facet) {

      // Facets core search moved into a separate project. Show a clean
      // message to notify users how to resolve their broken facets.
      if (substr($facet
        ->getFacetSourceId(), 0, 16) == 'core_node_search') {
        $project_link = Link::fromTextAndUrl('https://www.drupal.org/project/facets_core_search', Url::fromUri('https://www.drupal.org/project/facets_core_search'))
          ->toString();
        \Drupal::messenger()
          ->addError(t('Core search facets has been moved to a separate project. You need to download and enable this module from @project_link to continue using your core search facets.', [
          '@project_link' => $project_link,
        ]), 'error');
      }
      $form['facets'][$facet
        ->id()] = $this
        ->buildRow($facet);
      $form['facets'][$facet
        ->id()]['weight']['#attributes']['class'][] = $subgroup_class;
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
    '#button_type' => 'primary',
  ];
  return $form;
}