Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from slashrsm/another_serialization_strangeness
Browse files Browse the repository at this point in the history
Fix saving view in form state produces whole raft of strange bugs.
  • Loading branch information
slashrsm committed Jul 15, 2015
2 parents 25754fd + bdd8e2a commit bc7447b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Plugin/EntityBrowser/Widget/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ public function defaultConfiguration() {
public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
// TODO - do we need better error handling for view and view_display (in case
// either of those is nonexistent or display not of correct type)?
$storage = &$form_state->getStorage();
if (empty($storage['widget_view']) || $form_state->isRebuilding()) {
$storage['widget_view'] = $this->entityManager
->getStorage('view')
->load($this->configuration['view'])
->getExecutable();
}
/** @var \Drupal\views\ViewExecutable $view */
$view = $this->entityManager
->getStorage('view')
->load($this->configuration['view'])
->getExecutable();

if (!empty($this->configuration['arguments'])) {
if (!empty($aditional_widget_parameters['path_parts'])) {
Expand All @@ -53,13 +51,24 @@ public function getForm(array &$original_form, FormStateInterface $form_state, a
foreach ($this->configuration['arguments'] as $argument) {
$arguments[] = isset($aditional_widget_parameters['path_parts'][$argument]) ? $aditional_widget_parameters['path_parts'][$argument] : '';
}
$storage['widget_view']->setArguments(array_values($arguments));
$view->setArguments(array_values($arguments));
}
}

$form['view'] = $storage['widget_view']->executeDisplay($this->configuration['view_display']);
$form['view'] = $view->executeDisplay($this->configuration['view_display']);

$ids = [];
foreach ($view->result as $row_id => $row_result) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $row_result->_entity;
$ids[$row_id] = [
'id' => $entity->id(),
'type' => $entity->getEntityTypeId(),
];
}
$form_state->set('view_widget_rows', $ids);

if (empty($storage['widget_view']->field['entity_browser_select'])) {
if (empty($view->field['entity_browser_select'])) {
return [
// TODO - link to view admin page if allowed to.
'#markup' => t('Entity browser select form field not found on a view. Go fix it!'),
Expand Down Expand Up @@ -101,9 +110,9 @@ public static function processCheckbox(&$element, FormStateInterface $form_state
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$selected_rows = array_keys(array_filter($form_state->getValue('entity_browser_select')));
$entities = [];
$storage = $form_state->getStorage();
$ids = $form_state->get('view_widget_rows');
foreach ($selected_rows as $row) {
$entities[] = $storage['widget_view']->result[$row]->_entity;
$entities[] = $this->entityManager->getStorage($ids[$row]['type'])->load($ids[$row]['id']);
}

$this->selectEntities($entities);
Expand Down

0 comments on commit bc7447b

Please sign in to comment.