SocialPostListBuilder.php in Social Post 8.2
File
src/Entity/Controller/SocialPostListBuilder.php
View source
<?php
namespace Drupal\social_post\Entity\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialPostListBuilder extends EntityListBuilder {
protected $provider;
protected $urlGenerator;
protected $userEntity;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('url_generator'));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityStorageInterface $user_entity, UrlGeneratorInterface $url_generator) {
parent::__construct($entity_type, $storage);
$this->urlGenerator = $url_generator;
$this->userEntity = $user_entity;
}
public function setProvider($provider) {
$this->provider = $provider;
}
public function buildHeader() {
$header['social_id'] = $this
->t('Social Network ID');
$header['social_post_name'] = $this
->t('Screen name');
$header['user'] = $this
->t('User ID');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$provider = $entity
->getPluginId();
if ($provider == 'social_post_' . $this->provider) {
$row['provider_user_id'] = $entity
->getProviderUserId();
$link = $entity
->getLink();
if ($link
->isEmpty()) {
$row['social_post_name'] = $entity
->getName();
}
else {
$row['social_post_name'] = Link::fromTextAndUrl($link->title, $link
->getUrl());
}
$user = $this->userEntity
->load($entity
->getUserId());
$row['user'] = $user
->toLink();
return $row + parent::buildRow($entity);
}
return [];
}
public function getDefaultOperations(EntityInterface $entity) {
$provider = $entity
->getPluginId();
if ($provider == 'social_post_' . $this->provider) {
$operations = parent::getDefaultOperations($entity);
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.social_post.delete_form', [
'provider' => $this->provider,
'social_post' => $entity
->getId(),
'user' => FALSE,
]),
];
return $operations;
}
return [];
}
}