Created
July 22, 2018 11:03
-
-
Save erikfrerejean/078e2cf04f75f372a31a922bbb3495f2 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace Drupal\contextual\Plugin\views\field; | |
use Drupal\Component\Serialization\Json; | |
use Drupal\Component\Utility\Html; | |
use Drupal\Component\Utility\UrlHelper; | |
use Drupal\Core\Routing\RedirectDestinationTrait; | |
use Drupal\Core\Url; | |
use Drupal\views\Plugin\views\field\Links; | |
use Drupal\views\ResultRow; | |
/** | |
* Provides a handler that adds contextual links. | |
* | |
* @ingroup views_field_handlers | |
* | |
* @ViewsField("contextual_links") | |
*/ | |
class ContextualLinks extends Links { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function preRender(&$values) { | |
// Add a row plugin css class for the contextual link. | |
$class = 'contextual-region'; | |
if (!empty($this->view->style_plugin->options['row_class'])) { | |
$this->view->style_plugin->options['row_class'] .= " $class"; | |
} | |
else { | |
$this->view->style_plugin->options['row_class'] = $class; | |
} | |
} | |
/** | |
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render(). | |
* | |
* Renders the contextual fields. | |
* | |
* @param \Drupal\views\ResultRow $values | |
* The values retrieved from a single row of a view's query result. | |
* | |
* @see contextual_preprocess() | |
* @see contextual_contextual_links_view_alter() | |
*/ | |
public function render(ResultRow $values) { | |
$build = []; | |
$links = $this->getLinks(); | |
$key = 'view.' . $this->view->id() . '.' . $this->view->getDisplay()->getPluginId() . '.' . $values->index; | |
foreach ($links as $i => $link) { | |
$build['#contextual_links'][$key . '.' . $i]['route_parameters'] = $link['url']->getRouteParameters(); | |
} | |
return $build; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment