/** * {@inheritdoc} */ public static function settingsForm(FieldDefinitionInterface $field_definition) { $selection_handler_settings = $field_definition->getSetting('handler_settings') ?: array(); $view_settings = !empty($selection_handler_settings['view']) ? $selection_handler_settings['view'] : array(); $displays = Views::getApplicableViews('entity_reference_display'); // Filter views that list the entity type we want, and group the separate // displays by view. $entity_type = \Drupal::entityManager()->getDefinition($field_definition->getSetting('target_type')); $options = array(); foreach ($displays as $data) { list($view, $display_id) = $data; if ($view->storage->get('base_table') == $entity_type->getBaseTable()) { $name = $view->storage->get('id'); $display = $view->storage->get('display'); $options[$name . ':' . $display_id] = $name . ' - ' . $display[$display_id]['display_title']; } } // The value of the 'view_and_display' select below will need to be split // into 'view_name' and 'view_display' in the final submitted values, so // we massage the data at validate time on the wrapping element (not // ideal). $plugin = new static($field_definition); $form['view']['#element_validate'] = array(array($plugin, 'settingsFormValidate')); if ($options) { $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL; $form['view']['view_and_display'] = array('#type' => 'select', '#title' => t('View used to select the entities'), '#required' => TRUE, '#options' => $options, '#default_value' => $default, '#description' => '<p>' . t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>'); $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : ''; $form['view']['arguments'] = array('#type' => 'textfield', '#title' => t('View arguments'), '#default_value' => $default, '#required' => FALSE, '#description' => t('Provide a comma separated list of arguments to pass to the view.')); } else { $form['view']['no_view_help'] = array('#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array('@create' => url('admin/structure/views/add'), '@existing' => url('admin/structure/views'))) . '</p>'); } return $form; }
/** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { $links = array(); $views = Views::getApplicableViews('uses_menu_links'); foreach ($views as $data) { /** @var \Drupal\views\ViewExecutable $view */ list($view, $display_id) = $data; if ($result = $view->getMenuLinks($display_id)) { foreach ($result as $link_id => $link) { $links[$link_id] = $link + $base_plugin_definition; } } } return $links; }
/** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { $links = array(); $views = Views::getApplicableViews('uses_menu_links'); foreach ($views as $data) { list($view_id, $display_id) = $data; /** @var \Drupal\views\ViewExecutable $executable */ $executable = $this->viewStorage->load($view_id)->getExecutable(); if ($result = $executable->getMenuLinks($display_id)) { foreach ($result as $link_id => $link) { $links[$link_id] = $link + $base_plugin_definition; } } } return $links; }
/** * @covers ::getApplicableViews * * @dataProvider providerTestGetApplicableViews */ public function testGetApplicableViews($applicable_type, $expected) { $view_1 = new View(['id' => 'test_view_1', 'display' => ['default' => ['display_plugin' => 'default', 'display_options' => []], 'type_a' => ['display_plugin' => 'type_a', 'display_options' => []]]], 'view'); $view_2 = new View(['id' => 'test_view_2', 'display' => ['default' => ['display_plugin' => 'default', 'display_options' => []], 'type_b' => ['display_plugin' => 'type_b', 'display_options' => ['enabled' => TRUE]], 'type_b_2' => ['display_plugin' => 'type_b', 'display_options' => ['enabled' => FALSE]]]], 'view'); $view_3 = new View(['id' => 'test_view_3', 'display' => ['default' => ['display_plugin' => 'default', 'display_options' => []], 'type_a' => ['display_plugin' => 'type_a', 'display_options' => ['enabled' => FALSE]], 'type_d' => ['display_plugin' => 'type_d', 'display_options' => []]]], 'view'); $query = $this->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface'); $query->expects($this->exactly(2))->method('condition')->willReturnSelf(); $query->expects($this->once())->method('execute')->willReturn(['test_view_1', 'test_view_2', 'test_view_3']); $view_storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')->disableOriginalConstructor()->getMock(); $view_storage->expects($this->once())->method('getQuery')->willReturn($query); $view_storage->expects($this->once())->method('loadMultiple')->with(['test_view_1', 'test_view_2', 'test_view_3'])->will($this->returnValue(['test_view_1' => $view_1, 'test_view_2' => $view_2, 'test_view_3' => $view_3])); $entity_manager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface'); $entity_manager->expects($this->exactly(2))->method('getStorage')->with('view')->will($this->returnValue($view_storage)); $this->container->set('entity.manager', $entity_manager); $definitions = ['type_a' => ['type_a' => TRUE, 'type_b' => FALSE], 'type_b' => ['type_a' => FALSE, 'type_b' => TRUE]]; $display_manager = $this->getMock('Drupal\\Component\\Plugin\\PluginManagerInterface'); $display_manager->expects($this->once())->method('getDefinitions')->willReturn($definitions); $this->container->set('plugin.manager.views.display', $display_manager); $entity_query = new QueryFactory($entity_manager); $this->container->set('entity.query', $entity_query); $result = Views::getApplicableViews($applicable_type); $this->assertEquals($expected, $result); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $selection_handler_settings = $this->configuration['handler_settings']; $view_settings = !empty($selection_handler_settings['view']) ? $selection_handler_settings['view'] : array(); $displays = Views::getApplicableViews('entity_reference_display'); // Filter views that list the entity type we want, and group the separate // displays by view. $entity_type = $this->entityManager->getDefinition($this->configuration['target_type']); $view_storage = $this->entityManager->getStorage('view'); $options = array(); foreach ($displays as $data) { list($view_id, $display_id) = $data; $view = $view_storage->load($view_id); if (in_array($view->get('base_table'), [$entity_type->getBaseTable(), $entity_type->getDataTable()])) { $display = $view->get('display'); $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title']; } } // The value of the 'view_and_display' select below will need to be split // into 'view_name' and 'view_display' in the final submitted values, so // we massage the data at validate time on the wrapping element (not // ideal). $form['view']['#element_validate'] = array(array(get_called_class(), 'settingsFormValidate')); if ($options) { $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL; $form['view']['view_and_display'] = array('#type' => 'select', '#title' => $this->t('View used to select the entities'), '#required' => TRUE, '#options' => $options, '#default_value' => $default, '#description' => '<p>' . $this->t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>'); $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : ''; $form['view']['arguments'] = array('#type' => 'textfield', '#title' => $this->t('View arguments'), '#default_value' => $default, '#required' => FALSE, '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.')); } else { if ($this->currentUser->hasPermission('administer views') && $this->moduleHandler->moduleExists('views_ui')) { $form['view']['no_view_help'] = array('#markup' => '<p>' . $this->t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array('@create' => Url::fromRoute('views_ui.add')->toString(), '@existing' => Url::fromRoute('entity.view.collection')->toString())) . '</p>'); } else { $form['view']['no_view_help']['#markup'] = '<p>' . $this->t('No eligible views were found.') . '</p>'; } } return $form; }
/** * Return a list of all views and display IDs that have a menu entry. * * @return array * A list of arrays containing the $view and $display_id. * @code * array( * array($view, $display_id), * array($view, $display_id), * ); * @endcode */ protected function getApplicableMenuViews() { return Views::getApplicableViews('uses_menu_links'); }
/** * Return a list of all views and display IDs that have a menu entry. * * @return array * A list of arrays containing the $view and $display_id. * @code * array( * array($view, $display_id), * array($view, $display_id), * ); * @endcode */ protected function getApplicableMenuViews() { return Views::getApplicableViews('uses_hook_menu'); }
/** * Get select options for Views displays that support Calendar with set granularity. * * @param $granularity * * @return array */ protected function viewOptionsForGranularity($granularity) { $options = []; $view_displays = Views::getApplicableViews('uses_route'); foreach ($view_displays as $view_display) { list($view_id, $display_id) = $view_display; $view = View::load($view_id); $view_exec = $view->getExecutable(); if ($argument = CalendarHelper::getDateArgumentHandler($view_exec, $display_id)) { if ($argument->getGranularity() == $granularity) { $route_name = CalendarHelper::getDisplayRouteName($view_id, $display_id); $options[$route_name] = $view->label() . ' : ' . $view_exec->displayHandlers->get($display_id)->display['display_title']; } } } return $options; }
/** * Returns all views/display combinations with routes. * * @see \Drupal\views\Views::getApplicableViews() */ protected function getApplicableViews() { return Views::getApplicableViews('uses_route'); }