FlagViewsLinkField.php in Flag 8.4
File
src/Plugin/views/field/FlagViewsLinkField.php
View source
<?php
namespace Drupal\flag\Plugin\views\field;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\flag\FlaggingInterface;
use Drupal\flag\FlagLinkBuilderInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlagViewsLinkField extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $flag;
protected $flagLinkBuilder;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('flag.link_builder'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, FlagLinkBuilderInterface $flag_link_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->flagLinkBuilder = $flag_link_builder;
}
public function getFlag() {
if ($this->flag) {
return $this->flag;
}
elseif (isset($this->view->relationship[$this->options['relationship']])) {
return $this->view->relationship[$this->options['relationship']]
->getFlag();
}
return NULL;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['relationship'] = [
'default' => 'flag_relationship',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['relationship']['#default_value'] = $this->options['relationship'];
parent::buildOptionsForm($form, $form_state);
}
public function query() {
}
public function render(ResultRow $values) {
if ($values->_entity instanceof FlaggingInterface) {
$entity_type = $values->_entity
->getFlaggableType();
$entity_id = $values->_entity
->getFlaggableId();
$entity = $this
->getEntityTypeManager()
->getStorage($entity_type)
->load($entity_id);
$this->flag = $values->_entity
->getFlag();
}
else {
$entity = $this
->getParentRelationshipEntity($values);
}
return $this
->renderLink($entity, $values);
}
protected function getParentRelationshipEntity(ResultRow $values) {
$relationship_id = $this->options['relationship'];
$relationship_handler = $this->view->display_handler->handlers['relationship'][$relationship_id];
$parent_relationship_id = $relationship_handler->options['relationship'];
if ($parent_relationship_id == 'none') {
return $values->_entity;
}
elseif (isset($values->_relationship_entities[$parent_relationship_id])) {
return $values->_relationship_entities[$parent_relationship_id];
}
}
protected function renderLink(EntityInterface $entity, ResultRow $values) {
if (empty($entity)) {
return '';
}
return $this->flagLinkBuilder
->build($entity
->getEntityTypeId(), $entity
->id(), $this
->getFlag()
->id());
}
protected function getEntityTypeManager() {
if (!isset($this->entityTypeManager)) {
$this->entityTypeManager = \Drupal::service('entity_type.manager');
}
return $this->entityTypeManager;
}
}