Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created May 29, 2019 14:42
Show Gist options
  • Save barraponto/24fa57dab932ff8ad1703b6748137a86 to your computer and use it in GitHub Desktop.
Save barraponto/24fa57dab932ff8ad1703b6748137a86 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\views_selection_rendered_widget\Plugin\EntityReferenceSelection;
use Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection;
/**
* Plugin implementation of the 'selection' entity_reference.
*
* @EntityReferenceSelection(
* id = "views_rendered",
* label = @Translation("Views: Filter by an entity reference view and display rendered fields"),
* group = "views_rendered",
* weight = 1
* )
*/
class ViewsRenderedSelection extends ViewsSelection {
/**
* {@inheritdoc}
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$display_name = $this->getConfiguration()['view']['display_name'];
$arguments = $this->getConfiguration()['view']['arguments'];
$result = [];
if ($this->initializeView($match, $match_operator, $limit)) {
// Get the results.
$result = $this->view->executeDisplay($display_name, $arguments);
}
$return = [];
if ($result) {
foreach ($this->view->result as $row) {
$entity = $row->_entity;
$return[$entity->bundle()][$entity->id()] = render($this->view->rowPlugin->render($row));
}
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment