Commit ac55f66e authored by dragos-dumi's avatar dragos-dumi Committed by borisson_

Issue #2688899 by dragos-dumi: Order facets in the facet overview

parent 8e347224
......@@ -31,8 +31,10 @@ function facets_help($route_name, RouteMatchInterface $route_match) {
return $output;
case 'facets.overview':
return t('Below is a list of facets grouped by facetsources they are associated with. A facetsource is the instance where the facet does the actual filtering, for example a View on a Search API index.');
$output = '';
$output .= '<p>' . t('Below is a list of facets grouped by facetsources they are associated with. A facetsource is the instance where the facet does the actual filtering, for example a View on a Search API index.') . '</p>';
$output .= '<p>' . t('The facets weight can be changed with drag and drop withing the same facet source. Although you can drag and drop a facet under any facet source, this change will not be performed on save.') . '</p>';
return $output;
}
}
......
......@@ -2,25 +2,26 @@
namespace Drupal\facets;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Component\Utility\Html;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\FacetsSummaryInterface;
/**
* Builds a listing of facet entities.
*/
class FacetListBuilder extends ConfigEntityListBuilder {
class FacetListBuilder extends DraggableListBuilder {
/**
* {@inheritdoc}
*/
public function load() {
$entities = parent::load();
$this->sortAlphabetically($entities);
return $entities;
public function getFormId() {
return 'facets_overview';
}
/**
......@@ -103,51 +104,75 @@ class FacetListBuilder extends ConfigEntityListBuilder {
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\facets\FacetInterface $entity */
$row = parent::buildRow($entity);
$facet = $entity;
if ($facet instanceof FacetInterface) {
return array(
'data' => array(
'type' => array(
'data' => 'Facet',
'class' => array('facets-type'),
),
'title' => array(
'data' => array(
'#type' => 'link',
'#title' => $facet->label(),
'#suffix' => '<div>' . $entity->getFieldAlias() . ' - ' . $facet->getWidget()['type'] . '</div>',
) + $facet->toUrl('edit-form')->toRenderArray(),
'class' => array('search-api-title'),
$row = array(
'type' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-type'),
),
'operations' => $row['operations'],
),
'#type' => 'markup',
'#markup' => 'Facet',
),
'title' => array(
'#type' => 'link',
'#title' => $facet->label(),
'#suffix' => '<div>' . $entity->getFieldAlias() . ' - ' . $facet->getWidget()['type'] . '</div>',
'#attributes' => array(
'class' => array('search-api-title'),
),
) + $facet->toUrl('edit-form')->toRenderArray(),
'#attributes' => array(
'title' => $this->t('ID: @name', array('@name' => $facet->id())),
'class' => array('facet'),
);
}
elseif ($entity instanceof FacetsSummaryInterface) {
return array(
'data' => array(
'type' => array(
'data' => 'Facets Summary',
'class' => array('facets-summary-type'),
'class' => array(
'facet',
),
),
);
return array_merge_recursive($row, parent::buildRow($entity));
}
/**
* Builds an array of facet summary for display in the overview.
*/
public function buildFacetSummaryRow(FacetsSummaryInterface $entity) {
/** @var \Drupal\facets\FacetInterface $entity */;
$facet = $entity;
$row = parent::buildRow($entity);
return array(
'type' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-summary-type'),
),
),
'#type' => 'markup',
'#markup' => 'Facets Summary',
),
'title' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-title'),
)
),
'title' => array(
'data' => array(
'#type' => 'link',
'#title' => $facet->label(),
) + $facet->toUrl('edit-form')->toRenderArray(),
'#type' => 'link',
'#title' => $facet->label(),
'#attributes' => array(
'class' => array('search-api-title'),
),
'operations' => $row['operations'],
),
'#wrapper_attributes' => array(
'colspan' => 2,
),
) + $facet->toUrl('edit-form')->toRenderArray(),
'oprations' => $row['operations'],
'#attributes' => array(
'title' => $this->t('ID: @name', array('@name' => $facet->id())),
'class' => array('facet'),
);
}
return [];
'class' => array(
'facet',
),
),
);
}
/**
......@@ -155,32 +180,58 @@ class FacetListBuilder extends ConfigEntityListBuilder {
*/
public function buildFacetSourceRow(array $facet_source = []) {
return array(
'data' => array(
'type' => array(
'data' => 'Facet source',
'class' => array('facets-type'),
'type' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-type'),
),
),
'title' => array(
'data' => $facet_source['id'],
'#type' => 'markup',
'#markup' => 'Facet source',
),
'title' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-title'),
)
),
'operations' => array(
'data' => Link::createFromRoute(
$this->t('Configure'),
'entity.facets_facet_source.edit_form',
['facets_facet_source' => $facet_source['id']]
)->toRenderable(),
'#type' => 'markup',
'#markup' => $facet_source['id'],
'#wrapper_attributes' => array(
'colspan' => 2,
),
),
'class' => array('facet-source'),
'operations' => array(
'data' => Link::createFromRoute(
$this->t('Configure'),
'entity.facets_facet_source.edit_form',
['facets_facet_source' => $facet_source['id']]
)->toRenderable(),
),
'#attributes' => array(
'class' => array('facet-source', 'facet-source-' . $facet_source['id']),
'no_striping' => TRUE,
),
);
}
/**
* {@inheritdoc}
*/
public function render() {
public function buildForm(array $form, FormStateInterface $form_state) {
$groups = $this->loadGroups();
$form['facets'] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
'#empty' => $groups['lone_facets'] ? '' : $this->t('There are no facet sources or facets defined.'),
'#attributes' => array(
'class' => array(
'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'])) {
......@@ -193,42 +244,82 @@ class FacetListBuilder extends ConfigEntityListBuilder {
];
}
$list['#attached']['library'][] = 'facets/drupal.facets.admin_css';
$list['facet_sources'] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
'#rows' => array(),
'#empty' => $groups['lone_facets'] ? '' : $this->t('There are no facet sources or facets defined.'),
'#attributes' => array(
'class' => array(
'facets-groups-list',
),
),
);
$form['#attached']['library'][] = 'facets/drupal.facets.admin_css';
foreach ($groups['facet_source_groups'] as $facet_source_group) {
$list['facet_sources']['#rows'][$facet_source_group['facet_source']['id']] = $this->buildFacetSourceRow($facet_source_group['facet_source']);
$subgroup_class = Html::cleanCssIdentifier('facets-weight-' . $facet_source_group['facet_source']['id']);
$delta = round(count($facet_source_group['facets']) / 2);
$form['facets']['#tabledrag'][] = array(
'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 $i => $facet) {
$list['facet_sources']['#rows'][$facet->id()] = $this->buildRow($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'])) {
$list['lone_facets']['heading']['#markup'] = '<h3>' . $this->t('Facets not currently associated with any facet source') . '</h3>';
$list['lone_facets']['table'] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
'#rows' => array(),
$subgroup_class = 'facets-weight-lone-facets';
$form['facets']['#tabledrag'][] = array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
'subgroup' => $subgroup_class,
);
foreach ($groups['lone_facets'] as $entity) {
$list['lone_facets']['table']['#rows'][$entity->id()] = $this->buildRow($entity);
$form['facets']['lone_facets'] = array(
'type' => array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'facets-type'),
),
),
'#type' => 'markup',
'#markup' => '<h3>' . $this->t('Facets not currently associated with any facet source') . '</h3>',
),
'#wrapper_attributes' => array(
'colspan' => 4,
),
);
foreach ($facet_source_group['facets'] as $i => $facet) {
$form['facets'][$facet->id()] = $this->buildRow($facet);
$form['facets'][$facet->id()]['weight']['#attributes']['class'][] = $subgroup_class;
}
}
return $list;
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entities = $this->storage->loadMultiple(array_keys($form_state->getValue('facets')));
/** @var \Drupal\block\BlockInterface[] $entities */
foreach ($entities as $entity_id => $entity) {
$entity_values = $form_state->getValue(array('facets', $entity_id));
$entity->setWeight($entity_values['weight']);
$entity->save();
}
drupal_set_message(t('The facets have been updated.'));
}
/**
......@@ -242,15 +333,13 @@ class FacetListBuilder extends ConfigEntityListBuilder {
*/
public function loadGroups() {
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
$facets = $this->storage->loadMultiple();
$facets = $this->load();
$facets_summaries = [];
if (\Drupal::moduleHandler()->moduleExists('facets_summary')) {
$facets_summaries = FacetsSummary::loadMultiple();
}
$facet_sources = $facet_source_plugin_manager->getDefinitions();
$this->sortAlphabetically($facets);
$facet_source_groups = array();
foreach ($facet_sources as $facet_source) {
$facet_source_groups[$facet_source['id']] = [
......@@ -284,19 +373,4 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'lone_facets' => $facets,
];
}
/**
* Sorts an array of entities alphabetically.
*
* Will preserve the key/value association of the array.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityInterface[] $entities
* An array of config entities.
*/
protected function sortAlphabetically(array &$entities) {
uasort($entities, function (ConfigEntityInterface $a, ConfigEntityInterface $b) {
return strnatcasecmp($a->label(), $b->label());
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment