View source
<?php
namespace Drupal\social_event_invite\Plugin\views\field;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialEventInviteRecipientField extends FieldPluginBase {
use EntityTranslationRenderTrait;
protected $entityTypeManager;
protected $database;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->database = $database;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('database'));
}
public function usesGroupBy() {
return FALSE;
}
public function query() {
}
public function render(ResultRow $values) {
$build = [];
$recipient = $this
->checkEmailAndAccount($values->id, $values->users_field_data_event_enrollment__field_account_uid);
if (!empty($recipient['email'])) {
$build = $recipient['email'];
}
if (!empty($recipient['account'])) {
$account = $recipient['account'];
$storage = $this->entityTypeManager
->getStorage('profile');
$profile = $storage
->loadByUser($account, 'profile');
$entity = $this
->getEntity($values);
if ($entity && $profile) {
$build = [];
$entity = $this
->getEntityTranslation($entity, $values);
$view_builder = $this->entityTypeManager
->getViewBuilder('profile');
$build += $view_builder
->view($profile, 'table', $entity
->language()
->getId());
return $build;
}
}
return $build;
}
private function checkEmailAndAccount($enrollment_id, $account_id) {
$email = NULL;
$account = NULL;
if ($account_id) {
$account = $this->entityTypeManager
->getStorage('user')
->load($account_id);
return [
'email' => $account
->getEmail(),
'account' => $account,
];
}
$emailQuery = $this->database
->select('event_enrollment__field_event', 'eefev');
$emailQuery
->join('event_enrollment__field_email', 'eefem', 'eefem.entity_id = ' . $enrollment_id);
$emailQuery
->addField('eefem', 'field_email_value');
$email = $emailQuery
->execute()
->fetchField();
$userQuery = \Drupal::database()
->select('users_field_data', 'ufd');
$userQuery
->condition('mail', $email);
$userQuery
->addField('ufd', 'uid');
$userId = $userQuery
->execute()
->fetchField();
$account = NULL;
if ($userId) {
$account = $this->entityTypeManager
->getStorage('user')
->load($userId);
}
return [
'email' => $email,
'account' => $account,
];
}
public function getEntityTypeId() {
return $this
->getEntityTypeId();
}
protected function getLanguageManager() {
return $this
->getLanguageManager();
}
protected function getView() {
return $this
->getView();
}
}