commerce_inventory-8.x-1.0-alpha6/src/Plugin/views/field/CommerceInventoryAdjustmentDescription.php
src/Plugin/views/field/CommerceInventoryAdjustmentDescription.php
<?php
namespace Drupal\commerce_inventory\Plugin\views\field;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A handler to provide a field that is completely custom by the administrator.
*
* @ingroup views_field_handlers
*
* @ViewsField("commerce_inventory_adjustment_description")
*/
class CommerceInventoryAdjustmentDescription extends FieldPluginBase implements CacheableDependencyInterface {
use EntityTranslationRenderTrait;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new RenderedEntity object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
$view_display_storage = $this->entityManager->getStorage('entity_view_display');
$view_displays = $view_display_storage->loadMultiple($view_display_storage
->getQuery()
->condition('targetEntityType', $this->getEntityTypeId())
->execute());
$tags = [];
foreach ($view_displays as $view_display) {
$tags = array_merge($tags, $view_display->getCacheTags());
}
return $tags;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
/**
* {@inheritdoc}
*/
public function query() {
// We purposefully do not call parent::query() because we do not want the
// default query behavior for Views fields. Instead, let the entity
// translation renderer provide the correct query behavior.
if ($this->languageManager->isMultilingual()) {
$this->getEntityTranslationRenderer()->query($this->query, $this->relationship);
}
}
/**
* {@inheritdoc}
*/
public function getEntityTypeId() {
return $this->getEntityType();
}
/**
* {@inheritdoc}
*/
protected function getEntityManager() {
return $this->entityManager;
}
/**
* {@inheritdoc}
*/
protected function getLanguageManager() {
return $this->languageManager;
}
/**
* {@inheritdoc}
*/
protected function getView() {
return $this->view;
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_description'] = ['default' => FALSE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['link_description'] = [
'#type' => 'checkbox',
'#title' => $this->t('Link description'),
'#default_value' => $this->options['link_description'],
'#description' => $this->t('Link the product and the location in the description.'),
'#weight' => -100,
];
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
/** @var \Drupal\commerce_inventory\Entity\InventoryAdjustmentInterface $entity */
$entity = $this->getEntityTranslation($this->getEntity($values), $values);
$link = (array_key_exists('link_description', $this->options) && $this->options['link_description']);
return $entity->getDescription($link);
}
}
