Commit 56afe2e2 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3005040 by jcisio, akalam, idebr, mkalkbrenner, anfor, TuWebO,...

Issue #3005040 by jcisio, akalam, idebr, mkalkbrenner, anfor, TuWebO, anneke_vde, hardikpandya, borisson_, berliner, attiks: Add functionality for getHierarchy() to get different plugin types
parent fff31e4a
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -38,6 +38,16 @@ facets.facet.*:
    keep_hierarchy_parents_active:
      type: boolean
      label: 'Keep hierarchy parents active'
    hierarchy:
      type: mapping
      label: 'Hierarchy type'
      mapping:
        type:
          type: string
          label: 'Plugin id'
        config:
          type: facets.facet.[%parent.type]
          label: 'Configuration'
    expand_hierarchy:
      type: boolean
      label: 'Expand hierarchy'
+13 −0
Original line number Diff line number Diff line
@@ -196,3 +196,16 @@ function facets_update_8007() {
    $facet->save();
  }
}

/**
 * Support different hierarchy plugin types.
 */
function facets_update_8008() {
  $config_factory = \Drupal::configFactory();

  foreach ($config_factory->listAll('facets.facet.') as $facet_config_name) {
    $facet = $config_factory->getEditable($facet_config_name);
    $facet->set('hierarchy', ['type' => 'taxonomy', 'config' => []]);
    $facet->save(TRUE);
  }
}
+52 −3
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ use Drupal\facets\FacetInterface;
 *     "query_operator",
 *     "use_hierarchy",
 *     "keep_hierarchy_parents_active",
 *     "hierarchy",
 *     "expand_hierarchy",
 *     "enable_parent_when_child_gets_disabled",
 *     "hard_limit",
@@ -283,6 +284,15 @@ class Facet extends ConfigEntityBase implements FacetInterface {
   */
  protected $hierarchy_manager;

  /**
   * Cached information about the hierarchies available for this facet.
   *
   * @var \Drupal\facets\Hierarchy\HierarchyInterface[]
   *
   * @see getHierarchies()
   */
  protected $hierarchies;

  /**
   * The facet source config object.
   *
@@ -391,9 +401,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
   * {@inheritdoc}
   */
  public function getHierarchy() {
    // @todo do not hardcode on taxonomy, make this configurable (or better,
    // autoselected depending field type).
    return ['type' => 'taxonomy', 'config' => []];
    return $this->hierarchy;
  }

  /**
@@ -891,6 +899,37 @@ class Facet extends ConfigEntityBase implements FacetInterface {
    return $return_processors;
  }

  /**
   * {@inheritdoc}
   */
  public function getHierarchies() {
    if (is_array($this->hierarchies)) {
      return $this->hierarchies;
    }

    $this->hierarchies = [];

    $hierarchy_plugin_manager = $this->getHierarchyManager();

    foreach ($hierarchy_plugin_manager->getDefinitions() as $name => $hierarchy_definition) {
      if (class_exists($hierarchy_definition['class']) && empty($this->hierarchies[$name])) {

        /** @var \Drupal\facets\Hierarchy\HierarchyInterface $hierarchy */
        $hierarchy = $hierarchy_plugin_manager->createInstance($name);
        $this->hierarchies[$name] = $hierarchy;
      }
      elseif (!class_exists($hierarchy_definition['class'])) {
        \Drupal::logger('facets')
          ->warning('Hierarchy @id specifies a non-existing @class.', [
            '@id' => $name,
            '@class' => $hierarchy_definition['class'],
          ]);
      }
    }

    return $this->hierarchies;
  }

  /**
   * {@inheritdoc}
   */
@@ -986,6 +1025,16 @@ class Facet extends ConfigEntityBase implements FacetInterface {
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    if (!$this->getHierarchy()) {
      $this->setHierarchy('taxonomy');
    }
    parent::preSave($storage);
  }

  /**
   * {@inheritdoc}
   */
+10 −2
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ interface FacetInterface extends ConfigEntityInterface {
   *
   * @return array
   *   An associative array with the following structure:
   *   - id: The widget plugin id as a string.
   *   - type: The widget plugin id as a string.
   *   - config: The widget configuration as an array.
   */
  public function getWidget();
@@ -54,11 +54,19 @@ interface FacetInterface extends ConfigEntityInterface {
   *
   * @return array
   *   An associative array with the following structure:
   *   - id: The hierarchy plugin id as a string.
   *   - type: The hierarchy plugin id as a string.
   *   - config: The widget configuration as an array.
   */
  public function getHierarchy();

  /**
   * Returns an array of hierarchies with their configuration.
   *
   * @return \Drupal\facets\Hierarchy\HierarchyInterface[]
   *   An array of hierarchies.
   */
  public function getHierarchies();

  /**
   * Returns the facet hierarchy instance.
   *
+24 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\Hierarchy\HierarchyPluginBase;
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\search_api\Plugin\search_api\display\ViewsRest;
@@ -466,8 +467,22 @@ class FacetForm extends EntityForm {
        ':processor-url' => $processor_url->toString(),
      ]);
      $form['facet_settings']['use_hierarchy']['#description'] = $description;
      $form['facet_settings']['use_hierarchy']['#description'] .= '<br />';
      $form['facet_settings']['use_hierarchy']['#description'] .= '<strong>At this moment only hierarchical taxonomy terms are supported.</strong>';

      $hierarchy = $facet->getHierarchy();
      $options = array_map(function (HierarchyPluginBase $plugin) {
        return $plugin->getPluginDefinition()['label'];
      }, $facet->getHierarchies());
      $form['facet_settings']['hierarchy'] = [
        '#type' => 'select',
        '#title' => $this->t('Hierarchy type'),
        '#options' => $options,
        '#default_value' => $hierarchy ? $hierarchy['type'] : '',
        '#states' => [
          'visible' => [
            ':input[name="facet_settings[use_hierarchy]"]' => ['checked' => TRUE],
          ],
        ],
      ];
    }

    $form['facet_settings']['keep_hierarchy_parents_active'] = [
@@ -762,6 +777,13 @@ class FacetForm extends EntityForm {
        'keep_hierarchy_parents_active',
      ]
    ));
    $hierarchy_id = $form_state->getValue(['facet_settings', 'hierarchy']);
    $facet->setHierarchy($hierarchy_id, $form_state->getValue(
      [
        'facet_settings',
        $hierarchy_id,
      ]
    ));
    $facet->setExpandHierarchy($form_state->getValue(
      [
        'facet_settings',
Loading