Commit 083f7d4e authored by João Eduardo Ramos Costa's avatar João Eduardo Ramos Costa Committed by Markus Kalkbrenner
Browse files

Issue #3054068 by Antonnavi, joao.ramos, ilya.no, capysara, andypost,...

Issue #3054068 by Antonnavi, joao.ramos, ilya.no, capysara, andypost, mkalkbrenner: Allow the reset button to be either above or below the rest of the results
parent 196f96b2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -22,3 +22,6 @@ plugin.plugin_configuration.facets_summary_processor.reset_facets:
    link_text:
      type: label
      label: 'The text to show for the reset link.'
    position:
      type: string
      label: 'Position of the reset link.'
+24 −0
Original line number Diff line number Diff line
@@ -5,7 +5,10 @@
 * Update hooks for the facets summary module.
 */

use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\FacetsSummaryInterface;
use Drupal\facets_summary\Plugin\facets_summary\processor\ResetFacetsProcessor;

/**
 * Convert summaries on Search Api facet sources to use the display plugin.
@@ -26,3 +29,24 @@ function facets_summary_update_8001() {
    }
  }
}

/**
 * Set reset link position default value for all existing Facet summary.
 */
function facets_summary_update_8002(&$sandbox = NULL) {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'facets_summary', function ($facets_summary) {
    $update = FALSE;

    if ($facets_summary instanceof FacetsSummaryInterface) {
      $processor_settings = $facets_summary->getProcessorConfigs();

      if (isset($processor_settings['reset_facets'])) {
        $processor_settings['reset_facets']['settings']['position'] = ResetFacetsProcessor::POSITION_BEFORE;
        $facets_summary->addProcessor($processor_settings['reset_facets']);
        $update = TRUE;
      }
    }

    return $update;
  });
}
+45 −2
Original line number Diff line number Diff line
@@ -23,6 +23,21 @@ use Drupal\facets_summary\Processor\ProcessorPluginBase;
 */
class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessorInterface {

  /**
   * Indicates that reset link should be positioned before facet links.
   */
  const POSITION_BEFORE = 'before';

  /**
   * Indicates that reset link should be positioned after facet links.
   */
  const POSITION_AFTER = 'after';

  /**
   * Indicates that reset link should replace facet links.
   */
  const POSITION_REPLACE = 'replace';

  /**
   * {@inheritdoc}
   */
@@ -93,7 +108,20 @@ class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessor
        'facet-summary-item--clear',
      ],
    ];

    // Place link at necessary position.
    if ($configuration['settings']['position'] == static::POSITION_BEFORE) {
      array_unshift($build['#items'], $item);
    }
    elseif ($configuration['settings']['position'] == static::POSITION_AFTER) {
      $build['#items'][] = $item;
    }
    else {
      $build['#items'] = [
        $item,
      ];
    }

    return $build;
  }

@@ -110,6 +138,18 @@ class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessor
      '#default_value' => $config['link_text'],
    ];

    $build['position'] = [
      '#type' => 'select',
      '#options' => [
        static::POSITION_BEFORE => $this->t('Show reset link before facets links'),
        static::POSITION_AFTER => $this->t('Show reset link after facets links'),
        static::POSITION_REPLACE => $this->t('Show only reset link'),
      ],
      '#title' => $this->t('Position'),
      '#description' => $this->t('Set position of the link to display it before, after or instead of facets links.'),
      '#default_value' => $config['position'],
    ];

    return $build;
  }

@@ -117,7 +157,10 @@ class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessor
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return ['link_text' => ''];
    return [
      'link_text' => '',
      'position' => static::POSITION_BEFORE,
    ];
  }

}
+13 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\Tests\facets_summary\Functional;

use Drupal\Tests\facets\Functional\FacetsTestBase;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\Plugin\facets_summary\processor\ResetFacetsProcessor;
use Drupal\views\Views;

/**
@@ -395,9 +396,14 @@ class IntegrationTest extends FacetsTestBase {
    $this->assertSession()->pageTextContains($this->t('Facets Summary Owl has been updated.'));

    $this->assertSession()->fieldExists('facets_summary_settings[reset_facets][settings][link_text]');
    $this->submitForm(['facets_summary_settings[reset_facets][settings][link_text]' => 'Reset facets'], 'Save');
    $this->assertSession()->fieldExists('facets_summary_settings[reset_facets][settings][position]');
    $this->submitForm([
      'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset facets',
      'facets_summary_settings[reset_facets][settings][position]' => ResetFacetsProcessor::POSITION_BEFORE,
    ], 'Save');
    $this->assertSession()->pageTextContains($this->t('Facets Summary Owl has been updated.'));
    $this->assertSession()->fieldValueEquals('facets_summary_settings[reset_facets][settings][link_text]', 'Reset facets');
    $this->assertSession()->fieldValueEquals('facets_summary_settings[reset_facets][settings][position]', ResetFacetsProcessor::POSITION_BEFORE);
  }

  /**
@@ -483,7 +489,10 @@ class IntegrationTest extends FacetsTestBase {
        'reset_facets' => [
          'processor_id' => 'reset_facets',
          'weights' => ['build' => -10],
          'settings' => ['link_text' => 'Reset facets'],
          'settings' => [
            'link_text' => 'Reset facets',
            'position' => ResetFacetsProcessor::POSITION_BEFORE,
          ],
        ],
      ],
    ])->save();
@@ -564,6 +573,7 @@ class IntegrationTest extends FacetsTestBase {
      'facets[keywords][weight]' => 1,
      'facets_summary_settings[reset_facets][status]' => 1,
      'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset',
      'facets_summary_settings[reset_facets][settings][position]' => ResetFacetsProcessor::POSITION_BEFORE,
    ];
    $this->drupalGet('admin/config/search/facets/facet-summary/kepler/edit');
    $this->submitForm($summaries, 'Save');
@@ -612,6 +622,7 @@ class IntegrationTest extends FacetsTestBase {
      'facets[orval][weight]' => 0,
      'facets_summary_settings[reset_facets][status]' => 1,
      'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset',
      'facets_summary_settings[reset_facets][settings][position]' => ResetFacetsProcessor::POSITION_BEFORE,
    ];
    $this->drupalGet('admin/config/search/facets/facet-summary/trappist/edit');
    $this->submitForm($summaries, 'Save');
+10 −2
Original line number Diff line number Diff line
@@ -34,7 +34,12 @@ class ResetFacetsProcessorTest extends UnitTestCase {
    $container->set('string_translation', $string_translation->reveal());
    \Drupal::setContainer($container);

    $this->processor = new ResetFacetsProcessor(['settings' => ['link_text' => 'Text']], 'reset_facets', []);
    $this->processor = new ResetFacetsProcessor([
      'settings' => [
        'link_text' => 'Text',
        'position' => ResetFacetsProcessor::POSITION_BEFORE,
      ],
    ], 'reset_facets', []);
  }

  /**
@@ -66,7 +71,10 @@ class ResetFacetsProcessorTest extends UnitTestCase {
    $config = [
      'processor_id' => 'reset_facets',
      'weights' => [],
      'settings' => ['link_text' => 'Text'],
      'settings' => [
        'link_text' => 'Text',
        'position' => ResetFacetsProcessor::POSITION_BEFORE,
      ],
    ];
    $summary->addProcessor($config);

+1 −1

File changed.

Contains only whitespace changes.

Loading