Commit 7abc3fa0 authored by StryKaizer's avatar StryKaizer

Refactor display page and removed the empty behavior plugin

parent f9109e3c
......@@ -32,15 +32,6 @@ facetapi.facet.*:
sequence:
type: string
label: 'Widget plugin configurations'
empty_behavior:
type: string
label: 'Empty Behavior'
empty_behavior_configs:
type: sequence
label: 'Empty Behavior plugin configurations'
sequence:
type: sequence
label: 'Empty Behavior plugin configurations'
only_visible_when_facet_source_is_visible:
type: boolean
label: 'Show this facet only when the facet source is visible.'
......
......@@ -32,7 +32,13 @@
/*
* Facet API Display page
*/
#edit-processors .facetapi-processor-settings {
.facetapi-processor-settings-sorting {
margin-bottom: -7px;
margin-left: 20px;
margin-bottom: 25px;
margin-top: -16px;
}
.facetapi-processor-settings-facet {
margin-left: 20px;
margin-bottom: 20px;
}
\ No newline at end of file
......@@ -11,9 +11,6 @@ services:
plugin.manager.facetapi.processor:
class: Drupal\facetapi\Processor\ProcessorPluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@string_translation']
plugin.manager.facetapi.empty_behavior:
class: Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
parent: default_plugin_manager
facetapi.manager:
class: Drupal\facetapi\FacetManager\DefaultFacetManager
arguments:
......@@ -21,7 +18,6 @@ services:
- '@plugin.manager.facetapi.widget'
- '@plugin.manager.facetapi.facet_source'
- '@plugin.manager.facetapi.processor'
- '@plugin.manager.facetapi.empty_behavior'
- '@entity_type.manager'
facetapi.facet_context:
......
<?php
/**
* @file
* Contains \Drupal\facetapi\Annotation\FacetApiEmptyBehavior.
*/
namespace Drupal\facetapi\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API EmptyBehavior annotation.
*
* @see \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
* @see plugin_api
*
* @ingroup plugin_api
*
* @Annotation
*/
class FacetApiEmptyBehavior extends Plugin {
/**
* The empty behavior plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the empty behavior plugin.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* The empty behavior description.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
}
<?php
/**
* @file
* Contains Drupal\facetap\EmptyBehavior\EmptyBehaviorInterface.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Specifies the publicly available methods of an empty behavior plugin.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorPluginManager
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorInterface
* @see plugin_api
*/
interface EmptyBehaviorInterface extends PluginInspectionInterface, PluginFormInterface {
/**
* Returns the render array used for the facet that is empty, or has no items.
*
* @param array $facet_empty_behavior_configs
* Configuration for the empty behavior.
*
* @return
* The element's render array.
*/
public function build(array $facet_empty_behavior_configs);
}
<?php
/**
* @file
* Contains \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Common base class for empty behavior plugins.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorPluginManager
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorInterface
* @see plugin_api
*/
abstract class EmptyBehaviorPluginBase extends PluginBase implements EmptyBehaviorInterface, ContainerFactoryPluginInterface {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a EmptyBehaviorPluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $container->get('config.factory');
return new static($configuration, $plugin_id, $plugin_definition, $config_factory);
}
/**
* {@inheritdoc}
*/
public function build(array $config) {
return [];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
}
<?php
/**
* @file
* Contains \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Provides an EmptyBehavior plugin manager.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\EmptyBehavior\EmptyBehaviorInterface
*/
class EmptyBehaviorPluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/empty_behavior', $namespaces, $module_handler, 'Drupal\facetapi\EmptyBehavior\EmptyBehaviorInterface', 'Drupal\facetapi\Annotation\FacetApiEmptyBehavior');
}
}
......@@ -43,8 +43,6 @@ use Drupal\facetapi\FacetInterface;
* "widget",
* "widget_configs",
* "options",
* "empty_behavior",
* "empty_behavior_configs",
* "only_visible_when_facet_source_is_visible",
* },
* links = {
......@@ -93,13 +91,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $widget_configs;
/**
* Configuration for the empty behavior.
*
* @var string
*/
protected $empty_behavior;
/**
* An array of options configuring this index.
*
......@@ -327,21 +318,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->query_type_name;
}
/**
* {@inheritdoc}
*/
public function setFieldEmptyBehavior($behavior_id) {
$this->empty_behavior = $behavior_id;
return $this;
}
/**
* {@inheritdoc}
*/
public function getFieldEmptyBehavior() {
return $this->empty_behavior;
}
/**
* {@inheritdoc}
*/
......
......@@ -35,23 +35,6 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getFieldIdentifier();
/**
* Sets the empty_behavior id.
*
* @param $behavior_id
* The id for the empty behavior.
*
* @return mixed
*/
public function setFieldEmptyBehavior($behavior_id);
/**
* Get field empty_behavior.
*
* @return mixed
*/
public function getFieldEmptyBehavior();
/**
* Set field identifier.
*
......
......@@ -10,7 +10,6 @@ namespace Drupal\facetapi\FacetManager;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager;
use Drupal\facetapi\Exception\InvalidProcessorException;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginManager;
......@@ -51,13 +50,6 @@ class DefaultFacetManager {
*/
protected $processor_plugin_manager;
/**
* The empty behavior plugin manager.
*
* @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
*/
protected $empty_behavior_plugin_manager;
/**
* An array of facets that are being rendered.
*
......@@ -121,16 +113,14 @@ class DefaultFacetManager {
* @param \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager
* @param \Drupal\facetapi\FacetSource\FacetSourcePluginManager $facet_source_manager
* @param \Drupal\facetapi\Processor\ProcessorPluginManager $processor_plugin_manager
* @param \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager $empty_behavior_plugin_manager
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
*/
public function __construct(QueryTypePluginManager $query_type_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager, EmptyBehaviorPluginManager $empty_behavior_plugin_manager, EntityTypeManager $entity_type_manager) {
public function __construct(QueryTypePluginManager $query_type_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager, EntityTypeManager $entity_type_manager) {
$this->query_type_plugin_manager = $query_type_plugin_manager;
$this->widget_plugin_manager = $widget_plugin_manager;
$this->facet_source_manager = $facet_source_manager;
$this->processor_plugin_manager = $processor_plugin_manager;
$this->empty_behavior_plugin_manager = $empty_behavior_plugin_manager;
$this->facet_storage = $entity_type_manager->getStorage('facetapi_facet');
// Immediately initialize the facets. This can be done directly because the
......@@ -283,17 +273,15 @@ class DefaultFacetManager {
}
$facet->setResults($results);
// Returns the render array, this render array contains the empty behaviour
// if no results are found. If there are results we're going to initialize
// the widget from the widget plugin manager and return it's build method.
// No results behavior handling. Return a custom text or false depending on
// settings.
if (empty($facet->getResults())) {
// Get the empty behavior id and the configuration.
$facet_empty_behavior_configs = $facet->get('empty_behavior_configs');
$behavior_id = $facet->get('empty_behavior');
// Build the result using the empty behavior configuration.
$empty_behavior_plugin = $this->empty_behavior_plugin_manager->createInstance($behavior_id);
return $empty_behavior_plugin->build($facet_empty_behavior_configs);
$empty_behavior = $facet->getOption('empty_behavior');
if($empty_behavior['behavior'] == 'text'){
return ['#markup' => $empty_behavior['text']];
}else{
return;
}
}
// Let the widget plugin render the facet.
......
This diff is collapsed.
......@@ -254,7 +254,7 @@ class FacetForm extends EntityForm {
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id);
if ($config_form = $facet_source->buildConfigurationForm([], $form_state, $this->getEntity(), $facet_source)) {
$form['facet_source_configs'][$facet_source_id]['#type'] = 'fieldset';
$form['facet_source_configs'][$facet_source_id]['#type'] = 'container';
$form['facet_source_configs'][$facet_source_id]['#title'] = $this->t('%plugin settings', ['%plugin' => $facet_source->getPluginDefinition()['label']]);
$form['facet_source_configs'][$facet_source_id] += $config_form;
}
......@@ -305,7 +305,8 @@ class FacetForm extends EntityForm {
$facet->setWidget('links');
// Set default empty behaviour
$facet->set('empty_behavior', 'none');
$facet->setOption('empty_behavior', ['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
}
// Make sure the field identifier is copied from within the facet source
......@@ -336,41 +337,17 @@ class FacetForm extends EntityForm {
if ($is_new) {
if (\Drupal::moduleHandler()->moduleExists('block')) {
$message = $this->t('Go to the <a href=":block_overview">Block overview page</a> and add a new "Facet block". If this is your first and only facet, just adding that block make it link to this facet, if you have addded more facets already, please make sure to select the correct Facet to render.', [':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
$message = $this->t('Facet %name has been created. Go to the <a href=":block_overview">Block overview page</a> and add a new "Facet block". If this is your first and only facet, just adding that block make it link to this facet, if you have addded more facets already, please make sure to select the correct Facet to render.', ['%name' => $facet->getName(), ':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
drupal_set_message($message);
$form_state->setRedirect('entity.facetapi_facet.display_form', ['facetapi_facet' => $facet->id()]);
}
}else{
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
}
return $facet;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
// Only save the facet if the form doesn't need to be rebuilt.
if (!$form_state->isRebuilding()) {
try {
$facet = $this->getEntity();
// Check if this is a new facet, if so, ensure it has a widget.
if(!$facet->getWidget()){
$facet->save();
drupal_set_message(t('Facet %name has been created.', ['%name' => $facet->getName()]));
}else{
$facet->save();
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
}
}
catch (Exception $e) {
$form_state->setRebuild();
watchdog_exception('facetapi', $e);
drupal_set_message($this->t('The facet could not be saved.'), 'error');
}
}
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\empty_behavior\EmptyBehaviorNone.
*/
namespace Drupal\facetapi\Plugin\facetapi\empty_behavior;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase;
/**
* @FacetApiEmptyBehavior(
* id = "none",
* label = @Translation("Do not display facet"),
* description = @Translation("Do not display a facet when no results"),
* )
*/
class EmptyBehaviorNone extends EmptyBehaviorPluginBase {}
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\empty_behavior\EmptyBehaviorText.
*/
namespace Drupal\facetapi\Plugin\facetapi\empty_behavior;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase;
/**
* @FacetApiEmptyBehavior(
* id = "text",
* label = @Translation("Display text"),
* description = @Translation("Display a text when no results"),
* )
*/
class EmptyBehaviorText extends EmptyBehaviorPluginBase {
/**
* {@inheritdoc}
*/
public function build(array $config) {
return ['#markup' => $config['empty_text']['value']];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Get the facet.
$facet = $form_state->getFormObject()->getEntity();
// Get the configuration for the facet.
$config = $this->configFactory->get('facetapi.facet.' . $facet->id());
// Get the empty behavior configuration from the current facet.
$value_empty_text = isset($config->get('empty_behavior_configs')['empty_text']) ? $config->get('empty_behavior_configs')['empty_text']['value'] : '';
$value_empty_format = isset($config->get('empty_behavior_configs')['empty_text']) ? $config->get('empty_behavior_configs')['empty_text']['format'] : 'plain_text';
// Add the new field to the form.
$form['empty_text'] = [
'#type' => 'text_format',
'#title' => $this->t('Empty text'),
'#format' => $value_empty_format ?: 'plain_text',
'#editor' => FALSE,
'#default_value' => $value_empty_text ?: $value_empty_text,
];
return $form;
}
}
......@@ -54,7 +54,7 @@ class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcesso
'#type' => 'number',
'#min' => 1,
'#default_value' => !is_null($config) ? $config->getConfiguration()['minimum_items'] : $this->defaultConfiguration()['minimum_items'],
'#description' => $this->t('Hide block if the facet contains less than this number of results'),
'#description' => $this->t('Hide block if the facet contains less than this number of results.'),
);
return $build;
......
......@@ -17,8 +17,8 @@ use Drupal\user\Entity\User;
*
* @FacetApiProcessor(
* id = "uid_to_username_callback",
* label = @Translation("uid to username callback"),
* description = @Translation("Transform the uid to user name in facet results"),
* label = @Translation("Transform uid to username"),
* description = @Translation("Show the username instead, when the source field is a user id."),
* stages = {
* "build" = 5
* }
......
......@@ -85,12 +85,6 @@ class IntegrationTest extends FacetWebTestBase {
// Verify that facet blocks appear as expected.
$this->assertFacetBlocksAppear();
// Verify that the facet is visible when removing a facet for example.
$this->goToDeleteFacetPage("Test Facet Name");
$this->assertText('item');
$this->assertText('article');
// Show the facet only when the facet source is visible.
// @TODO Only for SearchApiViewsPage for the moment.
$this->setOptionShowOnlyWhenFacetSourceVisible("Test Facet name");
......@@ -200,10 +194,11 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertResponse(200);
// Configure the text for empty results behavior.
$this->drupalPostForm(NULL, ['empty_behavior' => 'text'], $this->t('Configure empty behavior'));
$this->drupalPostForm(NULL, ['empty_behavior_configs[empty_text][value]' => 'No results found for this block!'], $this->t('Configure empty behavior'));
$this->drupalPostForm(NULL, NULL, $this->t('Save'));
$edit = [
'facet_settings[empty_behavior]' => 'text',
'facet_settings[empty_behavior_text][value]' => 'No results found for this block!'
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
}
......@@ -220,10 +215,9 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertResponse(200);
$edit = [
'only_visible_when_facet_source_is_visible' => 1,
'facet_settings[only_visible_when_facet_source_is_visible]' => TRUE,
'widget' => 'links',
'widget_configs[show_numbers]' => '0',
// 'processors[query_string][status]' => '1',
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
}
......@@ -290,7 +284,7 @@ class IntegrationTest extends FacetWebTestBase {
// Make sure that the redirection back to the overview was successful and
// the newly added facet is shown on the overview page.
$this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name]));
$this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
}
......
<?php
/**
* @file Contains \Drupal\Tests\facetapi\Unit\EmptyBehavior\EmptyBehaviorPluginManagerTest.
*/
namespace Drupal\Tests\facetapi\Unit\EmptyBehavior;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager;
use Drupal\Tests\UnitTestCase;
use Zend\Stdlib\ArrayObject;
/**
* @group facetapi
*/
class EmptyBehaviorPluginManagerTest extends UnitTestCase {
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cache;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $discovery;
/**
* The plugin factory.
*
* @var \Drupal\Component\Plugin\Factory\DefaultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $factory;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin manager under test.
*
* @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
*/
public $sut;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->discovery = $this->getMock(DiscoveryInterface::class);
$this->factory = $this->getMockBuilder(DefaultFactory::class)
->disableOriginalConstructor()
->getMock();
$this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);
$this->cache = $this->getMock(CacheBackendInterface::class);
$namespaces = new ArrayObject();
$this->sut = new EmptyBehaviorPluginManager($namespaces, $this->cache, $this->moduleHandler);
$discovery_property = new \ReflectionProperty($this->sut, 'discovery');
$discovery_property->setAccessible(TRUE);
$discovery_property->setValue($this->sut, $this->discovery);
$factory_property = new \ReflectionProperty($this->sut, 'factory');
$factory_property->setAccessible(TRUE);
$factory_property->setValue($this->sut, $this->factory);
}
/**
* Tests plugin manager constructor.
*/
public function testConstruct() {
$namespaces = new ArrayObject();
$this->sut = new EmptyBehaviorPluginManager($namespaces, $this->cache, $this->moduleHandler);
}
/**
* Tests plugin manager's getDefinitions method.
*/
public function testGetDefinitions() {
$definitions = array(
'foo' => array(
'label' => $this->randomMachineName(),
),
);
$this->discovery->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
$this->assertSame($definitions, $this->sut->getDefinitions());
}
}
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