civicrm_entity-8.x-3.0-beta1/src/Plugin/views/field/ActivityAttachments.php
src/Plugin/views/field/ActivityAttachments.php
<?php
namespace Drupal\civicrm_entity\Plugin\views\field;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\civicrm_entity\CiviCrmApiInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Display file link base on CiviCRM.
*
* @ingroup views_field_handlers
*
* @ViewsField("civicrm_entity_activity_attachments")
*/
#[ViewsField("civicrm_entity_activity_attachments")]
class ActivityAttachments extends FieldPluginBase {
/**
* The CiviCRM API.
*
* @var \Drupal\civicrm_entity\CiviCrmApiInterface
*/
protected $civicrmApi;
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, CiviCrmApiInterface $civicrm_api) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->civicrmApi = $civicrm_api;
}
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, ?array &$options = NULL) {
parent::init($view, $display, $options);
$this->civicrmApi->civicrmInitialize();
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
if ($value) {
$items = [];
foreach (\CRM_Core_BAO_File::getEntityFile('civicrm_activity', $value) as $file) {
$items[] = [
'#type' => 'inline_template',
'#template' => $file['href'],
];
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
return NULL;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('civicrm_entity.api')
);
}
}
