Commit 9dfa950c authored by alexpott's avatar alexpott

Issue #2430813 by geertvd: Selecting the 'views' selection handler on an...

Issue #2430813 by geertvd: Selecting the 'views' selection handler on an entity_reference field causes a fatal error
parent 6743175a
......@@ -28,7 +28,7 @@ class EntityReferenceAdminTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('node', 'field_ui', 'entity_reference', 'path', 'taxonomy', 'block');
public static $modules = array('node', 'field_ui', 'entity_reference', 'path', 'taxonomy', 'block', 'views');
/**
......@@ -142,6 +142,14 @@ public function testFieldAdminHandler() {
$this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
$this->drupalGet($bundle_path . '/fields/' . $field_name);
$this->assertFieldByName('field[settings][handler_settings][filter][type]', '_none');
// Try to select the views handler.
$edit = array(
'field[settings][handler]' => 'views',
);
$this->drupalPostAjaxForm($bundle_path . '/fields/' . $field_name, $edit, 'field[settings][handler]');
$this->drupalPostForm(NULL, $edit, t('Save settings'));
$this->assertResponse(200);
}
......
......@@ -9,10 +9,12 @@
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -36,6 +38,20 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
*/
protected $entityManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The loaded View object.
*
......@@ -54,11 +70,17 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
}
/**
......@@ -69,7 +91,9 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')
$container->get('entity.manager'),
$container->get('module_handler'),
$container->get('current_user')
);
}
......@@ -120,12 +144,17 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
);
}
else {
$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'),
'@existing' => Url::fromRoute('entity.view.collection'),
)) . '</p>',
);
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'),
'@existing' => Url::fromRoute('entity.view.collection'),
)) . '</p>',
);
}
else {
$form['view']['no_view_help']['#markup'] = '<p>' . $this->t('No eligible views were found.') . '</p>';
}
}
return $form;
}
......@@ -243,13 +272,13 @@ public function entityQueryAlter(SelectInterface $query) {}
/**
* Element validate; Check View is valid.
*/
public function settingsFormValidate($element, FormStateInterface $form_state, $form) {
public static function settingsFormValidate($element, FormStateInterface $form_state, $form) {
// Split view name and display name from the 'view_and_display' value.
if (!empty($element['view_and_display']['#value'])) {
list($view, $display) = explode(':', $element['view_and_display']['#value']);
}
else {
$form_state->setError($element, $this->t('The views entity selection mode requires a view.'));
$form_state->setError($element, t('The views entity selection mode requires a view.'));
return;
}
......
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