diff --git a/js/block-ui.js b/js/block-ui.js
index d070468..0e47f80 100644
--- a/js/block-ui.js
+++ b/js/block-ui.js
@@ -9,9 +9,20 @@
$('.ctools-tabs li.active-tab').removeClass('active-tab');
$(this).parent().addClass('active-tab');
});
+ // Handle users who hit "enter" or "return" instead of tab.
+ $input.keypress(function( event ) {
+ if (event.which == 13) {
+ event.preventDefault();
+ $input.blur();
+ }
+ });
+ // Handle "blur"
$input.on('blur', function(e) {
var query = $(e.target).val().toLowerCase();
if (query) {
+ // If this is the first submitted query, add a new list
+ // item and attach the input's blur event to clicking the
+ // list item's anchor.
if (!$('#ctools-block-results-tab').length) {
$('.active-tab').removeClass('active-tab');
$list.prepend('
Results: "' + query + '"');
@@ -19,11 +30,14 @@
$input.blur();
})
}
+ // Update the existing results list item's text.
else {
$('#ctools-block-results-tab a').text('Results: "'+query+'"');
}
}
+ // If the query string is empty, let's remove all the things.
else {
+ $('#ctools-tabs-result #ctools-block-filter-message').remove();
$('#ctools-block-results-tab').remove();
$('.ctools-tabs ul.tab-categories li:first a').click();
}
diff --git a/src/Controller/BlockCategoryController.php b/src/Controller/BlockCategoryController.php
index f949835..993ee68 100644
--- a/src/Controller/BlockCategoryController.php
+++ b/src/Controller/BlockCategoryController.php
@@ -49,10 +49,18 @@ class BlockCategoryController extends ControllerBase {
public function listBlocks($plugin_id = 'icons', $category, $theme, Request $request) {
- /** @var \Drupal\ctools\BlockUIInterface $plugin */
- $plugin = $this->manager->createInstance($plugin_id);
$region = $request->query->get('region');
+ $category_url = Url::fromRoute('ctools.block.category', ['js' => 'nojs', 'theme' => $theme], ['query' => ['region' => $region]]);
$placement_url = Url::fromRoute('block.admin_add', ['js' => 'nojs', 'theme' => $theme], ['query' => ['region' => $region]]);
- return $plugin->categoryListing($this->contextRepository->getAvailableContexts(), $category, $placement_url, $request->request->get('js'));
+ $configuration = [
+ 'url' => [
+ 'category' => $category_url,
+ 'placement' => $placement_url,
+ ],
+ 'contexts' => $this->contextRepository->getAvailableContexts()
+ ];
+ /** @var \Drupal\ctools\Plugin\BlockUI\Icons $plugin */
+ $plugin = \Drupal::service('plugin.manager.ctools.block_ui')->createInstance($plugin_id, $configuration);
+ return $plugin->categoryListing($category, $request->request->get('js'));
}
}
diff --git a/src/Controller/BlockLibraryController.php b/src/Controller/BlockLibraryController.php
index d8327e1..2405604 100644
--- a/src/Controller/BlockLibraryController.php
+++ b/src/Controller/BlockLibraryController.php
@@ -20,13 +20,19 @@ class BlockLibraryController extends CoreBlockLibraryController {
* {@inheritdoc}
*/
public function listBlocks($plugin_id = 'icons', Request $request, $theme) {
- /** @var \Drupal\ctools\Plugin\BlockUI\Icons $plugin */
- $plugin = \Drupal::service('plugin.manager.ctools.block_ui')->createInstance($plugin_id);
$region = $request->query->get('region');
$category_url = Url::fromRoute('ctools.block.category', ['js' => 'nojs', 'theme' => $theme], ['query' => ['region' => $region]]);
$placement_url = Url::fromRoute('block.admin_add', ['js' => 'nojs', 'theme' => $theme], ['query' => ['region' => $region]]);
- $build = $plugin->buildListing($this->contextRepository->getAvailableContexts(), $category_url, $placement_url);
- return $build;
+ $configuration = [
+ 'url' => [
+ 'category' => $category_url,
+ 'placement' => $placement_url,
+ ],
+ 'contexts' => $this->contextRepository->getAvailableContexts()
+ ];
+ /** @var \Drupal\ctools\Plugin\BlockUI\Icons $plugin */
+ $plugin = \Drupal::service('plugin.manager.ctools.block_ui')->createInstance($plugin_id, $configuration);
+ return $plugin->buildListing();
}
}
diff --git a/src/Form/BlockSearchFilter.php b/src/Form/BlockSearchFilter.php
index 10df1e7..8733c64 100644
--- a/src/Form/BlockSearchFilter.php
+++ b/src/Form/BlockSearchFilter.php
@@ -15,6 +15,7 @@ use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
+use Drupal\ctools\Plugin\BlockUIInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockSearchFilter extends FormBase {
@@ -31,7 +32,10 @@ class BlockSearchFilter extends FormBase {
return 'ctools_block_search_filter';
}
- public function buildForm(array $form, FormStateInterface $form_state, Url $placement_url = NULL, array $contexts = []) {
+ public function buildForm(array $form, FormStateInterface $form_state, BlockUIInterface $plugin = NULL) {
+ if (!$plugin) {
+ throw new \Exception('A BlockUIInterface implementing plugin must be provided to this form.');
+ }
$form['filter'] = [
'#type' => 'search',
'#title' => $this->t('Filter'),
@@ -47,54 +51,52 @@ class BlockSearchFilter extends FormBase {
'callback' => '::search',
],
];
- $form['url'] = [
- '#type' => 'value',
- '#value' => $placement_url,
- ];
- $form['contexts'] = [
+ $form['plugin'] = [
'#type' => 'value',
- '#value' => $contexts,
+ '#value' => $plugin,
];
return $form;
}
- public function submitForm(array &$form, FormStateInterface $form_state) {
- // TODO: Implement submitForm() method.
- }
+ public function submitForm(array &$form, FormStateInterface $form_state) {}
public function search(array &$form, FormStateInterface $form_state) {
$string = $form_state->getValue('filter');
- $placement_url = $form_state->getValue('url');
- $contexts = $form_state->getValue('contexts');
- $definitions = $this->manager->getDefinitionsForContexts($contexts);
- $definitions = $this->manager->getSortedDefinitions($definitions);
- $items = [];
- foreach ($definitions as $plugin_id => $definition) {
- if (stripos((string) $definition['admin_label'], $string) !== FALSE) {
- $new_placement = clone $placement_url->setRouteParameter('plugin_id', $plugin_id);
- $link = [
- '#type' => 'link',
- '#title' => $definition['admin_label'],
- '#url' => $new_placement,
- '#attributes' => [
- 'class' => ['use-ajax'],
- 'data-dialog-type' => 'modal',
- 'data-dialog-options' => Json::encode([
- 'width' => 950,
- ]),
+ /** @var \Drupal\ctools\Plugin\BlockUIInterface $plugin */
+ $plugin = $form_state->getValue('plugin');
+ if ($string) {
+ $contexts = $plugin->getProvidedContexts();
+ $definitions = $this->manager->getDefinitionsForContexts($contexts);
+ $definitions = $this->manager->getSortedDefinitions($definitions);
+ foreach ($definitions as $plugin_id => $definition) {
+ if (stripos((string) $definition['admin_label'], $string) === FALSE) {
+ unset($definitions[$plugin_id]);
+ }
+ }
+ $items = $plugin->styleResults($definitions);
+ if (!$items) {
+ $items = [
+ '#prefix' => '',
+ '#suffix' => '
',
+ '#theme' => 'status_messages',
+ '#message_list' => [
+ 'error' => [
+ $this->t('No results found for the submitted query.'),
+ ]
+ ],
+ '#status_headings' => [
+ 'status' => t('Status message'),
+ 'error' => t('Error message'),
+ 'warning' => t('Warning message'),
],
- ];
- $items[$plugin_id] = [
- '#theme' => 'ctools_block_definition_icon',
- '#link' => $link,
- '#definition' => $definition,
];
}
+ $response = new AjaxResponse();
+ $response->addCommand(new RemoveCommand('#ctools-tabs-result #ctools-block-filter-message'));
+ $response->addCommand(new RemoveCommand('#ctools-tabs-result .block-option'));
+ $response->addCommand(new AppendCommand('#ctools-tabs-result', $items));
+ return $response;
}
- $response = new AjaxResponse();
- $response->addCommand(new RemoveCommand('#ctools-tabs-result .block-option'));
- $response->addCommand(new AppendCommand('#ctools-tabs-result', $items));
- return $response;
}
}
diff --git a/src/Plugin/BlockUI/Icons.php b/src/Plugin/BlockUI/Icons.php
index 485cf58..9304084 100644
--- a/src/Plugin/BlockUI/Icons.php
+++ b/src/Plugin/BlockUI/Icons.php
@@ -6,7 +6,6 @@
namespace Drupal\ctools\Plugin\BlockUI;
-
use Drupal\Component\Serialization\Json;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
@@ -24,47 +23,46 @@ use Drupal\ctools\Plugin\BlockUIBase;
*/
class Icons extends BlockUIBase {
- /**
- * {@inheritdoc}
- */
- public function getRouteParameterKeys() {
- return ['category'];
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, \Drupal\Core\Block\BlockManagerInterface $manager) {
+ if (!$configuration['url']['category'] instanceof Url) {
+ throw new \Exception('A proper category url was not provided for this plugin.');
+ }
+ if (!$configuration['url']['placement'] instanceof Url) {
+ throw new \Exception('A proper placement url was not provided for this plugin.');
+ }
+ parent::__construct($configuration, $plugin_id, $plugin_definition, $manager);
}
/**
* {@inheritdoc}
*/
- public function buildListing(array $contexts = array(), Url $category_url, Url $placement_url) {
- $build = \Drupal::formBuilder()->getForm('\Drupal\ctools\Form\BlockSearchFilter', $placement_url, $contexts);
+ public function buildListing() {
+ $build = \Drupal::formBuilder()->getForm('\Drupal\ctools\Form\BlockSearchFilter', $this);
$build['#attached']['library'][] = 'ctools/block.ui';
$build['tab_results'] = [
'#theme' => 'ctools_block_listing',
- '#blocks' => $this->getItemsByContext($contexts, $category_url, $placement_url),
+ '#blocks' => $this->getItemsByContext(),
];
return $build;
}
/**
- * @param array $contexts
- * @param \Drupal\Core\Url $category_url
- * @param \Drupal\Core\Url $placement_url
- *
* @return array
*/
- protected function getItemsByContext(array $contexts = [], Url $category_url, Url $placement_url) {
- $definitions = $this->manager->getDefinitionsForContexts($contexts);
+ protected function getItemsByContext() {
+ $definitions = $this->manager->getDefinitionsForContexts($this->getProvidedContexts());
$definitions = $this->manager->getSortedDefinitions($definitions);
$categories = [];
$items = [];
foreach ($definitions as $definition) {
$category = (string) $definition['category'];
- if (isset($categories[$category])) {
- continue;
- }
- $categories[$category] = $definition['category'];
// @todo let's make a static utility class for doing this.
$machine_name = str_replace([' ', '-'], '_', strtolower($category));
- $new_category = clone $category_url->setRouteParameter('category', $machine_name);
+ if (isset($categories[$machine_name])) {
+ continue;
+ }
+ $categories[$machine_name] = $category;
+ $new_category = $this->getUrl('category')->setRouteParameter('category', $machine_name);
$items[$machine_name] = [
'name' => $machine_name,
'value' => $category,
@@ -75,63 +73,28 @@ class Icons extends BlockUIBase {
],
];
}
- $category = array_shift($categories);
+ $category = array_shift(array_keys($categories));
foreach ($definitions as $plugin_id => $definition) {
- if ($definition['category'] == $category) {
- $machine_name = str_replace([' ', '-'], '_', strtolower($category));
- $new_placement = clone $placement_url->setRouteParameter('category', $machine_name);
- $new_placement->setRouteParameter('plugin_id', $plugin_id);
- $link = [
- '#type' => 'link',
- '#title' => $definition['admin_label'],
- '#url' => $new_placement,
- '#attributes' => [
- 'class' => ['use-ajax'],
- 'data-dialog-type' => 'modal',
- 'data-dialog-options' => Json::encode([
- 'width' => 950,
- ]),
- ],
- ];
- $items[$machine_name]['options'][$plugin_id] = [
- '#theme' => 'ctools_block_definition_icon',
- '#link' => $link,
- '#definition' => $definition,
- ];
+ $machine_name = str_replace([' ', '-'], '_', strtolower($definition['category']));
+ if ($machine_name != $category) {
+ unset($definitions[$plugin_id]);
}
}
+ $items[$category]['options'] = $this->styleResults($definitions);
return $items;
}
- public function categoryListing(array $contexts = [], $category, Url $placement_url, $ajax = FALSE) {
- $definitions = $this->manager->getDefinitionsForContexts($contexts);
+ public function categoryListing($category, $ajax = FALSE) {
+ $definitions = $this->manager->getDefinitionsForContexts($this->getProvidedContexts());
$definitions = $this->manager->getSortedDefinitions($definitions);
- $items = [];
foreach ($definitions as $plugin_id => $definition) {
$machine_name = str_replace([' ', '-'], '_', strtolower($definition['category']));
- if ($machine_name == $category) {
- $new_placement = clone $placement_url->setRouteParameter('category', $machine_name);
- $new_placement->setRouteParameter('plugin_id', $plugin_id);
- $link = [
- '#type' => 'link',
- '#title' => $definition['admin_label'],
- '#url' => $new_placement,
- '#attributes' => [
- 'class' => ['use-ajax'],
- 'data-dialog-type' => 'modal',
- 'data-dialog-options' => Json::encode([
- 'width' => 950,
- ]),
- ],
- ];
- $items[$plugin_id] = [
- '#theme' => 'ctools_block_definition_icon',
- '#link' => $link,
- '#definition' => $definition,
- ];
+ if ($machine_name != $category) {
+ unset($definitions[$plugin_id]);
}
}
+ $items = $this->styleResults($definitions);
if ($ajax) {
$response = new AjaxResponse();
$response->addCommand(new RemoveCommand('#ctools-tabs-result .block-option'));
@@ -141,4 +104,32 @@ class Icons extends BlockUIBase {
return $items;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function styleResults(array $definitions = []) {
+ $items = [];
+ foreach ($definitions as $plugin_id => $definition) {
+ $new_placement = $this->getUrl('placement')->setRouteParameter('plugin_id', $plugin_id);
+ $link = [
+ '#type' => 'link',
+ '#title' => $definition['admin_label'],
+ '#url' => $new_placement,
+ '#attributes' => [
+ 'class' => ['use-ajax'],
+ 'data-dialog-type' => 'modal',
+ 'data-dialog-options' => Json::encode([
+ 'width' => 950,
+ ]),
+ ],
+ ];
+ $items[$plugin_id] = [
+ '#theme' => 'ctools_block_definition_icon',
+ '#link' => $link,
+ '#definition' => $definition,
+ ];
+ }
+ return $items;
+ }
+
}
diff --git a/src/Plugin/BlockUIBase.php b/src/Plugin/BlockUIBase.php
index 56bf39b..b77675a 100644
--- a/src/Plugin/BlockUIBase.php
+++ b/src/Plugin/BlockUIBase.php
@@ -7,7 +7,10 @@
namespace Drupal\ctools\Plugin;
+use Drupal\Component\Plugin\ConfigurablePluginInterface;
+use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Component\Plugin\PluginBase;
+use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -17,20 +20,38 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for Block User Interface plugins.
*/
-abstract class BlockUIBase extends PluginBase implements BlockUIInterface, ContainerFactoryPluginInterface {
+abstract class BlockUIBase extends PluginBase implements BlockUIInterface, ConfigurablePluginInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
use DependencySerializationTrait;
/**
+ * The block manager.
+ *
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $manager;
/**
+ * The array of available contexts.
+ *
+ * @var \Drupal\Component\Plugin\Context\ContextInterface[]
+ */
+ protected $contexts;
+
+ /**
* BlockUIBase constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
+ $contexts = isset($configuration['contexts']) ? $configuration['contexts'] : [];
+ foreach ($contexts as $context) {
+ if (!$context instanceof ContextInterface) {
+ throw new \Exception('All contexts must be instance of \Drupal\Component\Plugin\Context\ContextInterface');
+ }
+ }
+ $this->contexts = $contexts;
+ unset($configuration['contexts']);
+ $this->setConfiguration($configuration);
$this->manager = $manager;
}
@@ -41,4 +62,53 @@ abstract class BlockUIBase extends PluginBase implements BlockUIInterface, Conta
return new static($configuration, $plugin_id, $plugin_definition, $container->get('plugin.manager.block'));
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getUrl($type) {
+ if (!isset($this->configuration['url'][$type])) {
+ throw new \Exception(sprintf('A proper %s url does not exist for this plugin.', $type));
+ }
+ return clone $this->configuration['url'][$type];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getProvidedContexts() {
+ return $this->contexts;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return array();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getConfiguration() {
+ return $this->configuration;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setConfiguration(array $configuration) {
+ $this->configuration = NestedArray::mergeDeep(
+ $this->defaultConfiguration(),
+ $configuration
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function calculateDependencies() {
+ return array();
+ }
+
+
}
diff --git a/src/Plugin/BlockUIInterface.php b/src/Plugin/BlockUIInterface.php
index 3cedf70..746b27a 100644
--- a/src/Plugin/BlockUIInterface.php
+++ b/src/Plugin/BlockUIInterface.php
@@ -16,24 +16,30 @@ use Drupal\Core\Url;
interface BlockUIInterface extends PluginInspectionInterface {
/**
- * Provides a list of route parameter keys required for this plugin.
+ * @param string $type
*
- * @return array
+ * @return \Drupal\Core\Url
*/
- public function getRouteParameterKeys();
+ public function getUrl($type);
/**
* Returns a render array for this block UI plugin.
*
- * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
- * An array of ContextInterface objects to limit available blocks by.
- * @param \Drupal\Core\Url $category_url
- * The Url object for category urls.
- * @param \Drupal\Core\Url $placement_url
- *
- *
* @return array
*/
- public function buildListing(array $contexts = array(), Url $category_url, Url $placement_url);
+ public function buildListing();
+
+ /**
+ * @param array $definitions
+ * The array of block plugin definitions
+ *
+ * @return mixed
+ */
+ public function styleResults(array $definitions = []);
+
+ /**
+ * @return array|\Drupal\Component\Plugin\Context\ContextInterface[]
+ */
+ public function getProvidedContexts();
}