You are here

ActivityAttachments.php in CiviCRM Entity 8.3

File

src/Plugin/views/field/ActivityAttachments.php
View source
<?php

namespace Drupal\civicrm_entity\Plugin\views\field;

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")
 */
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'));
  }

}

Classes

Namesort descending Description
ActivityAttachments Display file link base on CiviCRM.