Last active
December 22, 2015 15:16
-
-
Save filipengberg/73d1696694bb3e869efb to your computer and use it in GitHub Desktop.
Drupal 8: DefaultSelection Plugin subclass for filtering selectable Views by tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\example\Plugin\EntityReferenceSelection\ViewSelection. | |
| */ | |
| namespace Drupal\example\Plugin\EntityReferenceSelection; | |
| use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; | |
| use Drupal\Core\Form\FormStateInterface; | |
| use Drupal\views\Views; | |
| /** | |
| * Provides specific access control for the View entity type. | |
| * | |
| * @EntityReferenceSelection( | |
| * id = "default:view", | |
| * label = @Translation("View selection"), | |
| * entity_types = {"view"}, | |
| * group = "default", | |
| * weight = 1 | |
| * ) | |
| */ | |
| class ViewSelection extends DefaultSelection { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
| $form = parent::buildConfigurationForm($form, $form_state); | |
| $selection_handler_settings = $this->configuration['handler_settings']; | |
| // Get all view tags | |
| $views = Views::getAllViews(); | |
| $view_options = []; | |
| foreach ($views as $view) { | |
| $tag = $view->get('tag'); | |
| if ($tag) { | |
| $view_options[$tag] = $tag; | |
| } | |
| } | |
| // Add tag selection to form | |
| $form['target_views'] = array( | |
| '#type' => 'checkboxes', | |
| '#title' => $this->t('View tags'), | |
| '#description' => $this->t('Filter which views should be selectable by View tags.'), | |
| '#options' => $view_options, | |
| '#default_value' => (array) $selection_handler_settings['target_views'], | |
| '#required' => FALSE, | |
| '#size' => 6, | |
| '#multiple' => TRUE, | |
| '#element_validate' => [[get_class($this), 'elementValidateFilter']], | |
| ); | |
| return $form; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { | |
| parent::validateConfigurationForm($form, $form_state); | |
| // Allow all views to be selected if no tag was chosen | |
| if ($form_state->getValue(['settings', 'handler_settings', 'target_views']) === []) { | |
| $form_state->setValue(['settings', 'handler_settings', 'target_views'], NULL); | |
| } | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { | |
| $query = parent::buildEntityQuery($match, $match_operator); | |
| $handler_settings = $this->configuration['handler_settings']; | |
| // Filter selectable views by tag | |
| if (isset($handler_settings['target_views']) && is_array($handler_settings['target_views'])) { | |
| if ($handler_settings['target_views'] === []) { | |
| $query->condition('id', NULL, '='); | |
| return $query; | |
| } | |
| else { | |
| $query->condition('tag', $handler_settings['target_views'], 'IN'); | |
| } | |
| } | |
| return $query; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment