Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCC-59: Views integration with pcc site entity. #4

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions pcx_connect.views.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* @file
* Views hook implementations for the PCX Connect module.
*/

use Drupal\pcx_connect\Entity\PccSite;
use Drupal\pcx_connect\PccSiteViewHelper;

/**
* Implements hook_views_data().
*/
function pcx_connect_views_data() {
$data = [];

$pcc_sites = PccSite::loadMultiple();
if ($pcc_sites) {
$translation = \Drupal::translation();
foreach ($pcc_sites as $pcc_site) {
$id = $pcc_site->getEntityTypeId();
$name = $pcc_site->label();
$table = &$data[$pcc_site->id()];
// Base data.
$table['table']['group'] = $translation->translate('PCC Site - @name', ['@name' => $name]);
$table['table']['provider'] = $id;

// Locate the config entity type's ID.
$entityType = \Drupal::entityTypeManager()
->getStorage($id)
->getEntityType();
$id_key = $entityType->getKey('id');

// Get entity fields.
$fields = PccSiteViewHelper::getMapping($id);
if (empty($fields[$id_key])) {
continue;
}
if (empty($fields[$id_key]['label'])) {
continue;
}

$table['table']['base'] = [
'field' => strtolower($fields[$id_key]['label']),
'index' => strtolower($fields[$id_key]['label']),
'title' => $translation->translate('PCC Site - @name', ['@name' => $name]),
'help' => $translation->translate('Use the ConfigEntity @name to view the data.', ['@name' => $name]),
'query_id' => 'pcc_site_view_query',
];

$table['title'] = [
'title' => $translation->translate('Title'),
'help' => $translation->translate('Article title'),
'field' => [
'id' => 'standard',
'click sortable' => TRUE,
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];

$table['content'] = [
'title' => $translation->translate('Content'),
'help' => $translation->translate('Article Content'),
'field' => [
'id' => 'standard',
'click sortable' => FALSE,
],
'filter' => [
'id' => 'string',
],
];

$table['snippet'] = [
'title' => $translation->translate('Snippet'),
'help' => $translation->translate('Article Snippet'),
'field' => [
'id' => 'standard',
'click sortable' => FALSE,
],
'filter' => [
'id' => 'string',
],
];

$table['publishedAt'] = [
'title' => $translation->translate('Published At'),
'help' => $translation->translate('Article publish time'),
'field' => [
'id' => 'date',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];

$table['updatedAt'] = [
'title' => $translation->translate('Updated At'),
'help' => $translation->translate('Article update time'),
'field' => [
'id' => 'date',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
}
}
return array_filter($data);
}
5 changes: 3 additions & 2 deletions src/Controller/ApiStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* Api Status Controller responsible for returning response for pcc site status endpoint.
* Api Status Controller for returning response for pcc site status endpoint.
*/
class ApiStatusController extends ControllerBase {

/**
* Returns empty response.
*
* @return JsonResponse
* @return \Symfony\Component\HttpFoundation\JsonResponse
* Empty JSON response.
*/
public function emptyResponse() {
return new JsonResponse();
}

}
46 changes: 46 additions & 0 deletions src/Entity/PccSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\pcx_connect\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\pcx_connect\PccSiteInterface;

/**
Expand Down Expand Up @@ -39,6 +40,27 @@
* )
*/
class PccSite extends ConfigEntityBase implements PccSiteInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $module_handler;

/**
* The views data service.
*
* @var \Drupal\views\ViewsData
*/
protected $views_data;


/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache_discovery;

/**
* The PCC Site ID.
Expand All @@ -54,4 +76,28 @@ class PccSite extends ConfigEntityBase implements PccSiteInterface {
*/
protected $label;

/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type) {
$this->module_handler = \Drupal::service('module_handler');
$this->views_data = \Drupal::service('views.views_data');
$this->cache_discovery = \Drupal::service('cache.discovery');

parent::__construct($values, $entity_type);
}

/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);

// Clear cache to populate the PCC site in views.
if ($this->module_handler->moduleExists('views')) {
$this->views_data->clear();
$this->cache_discovery->delete('views:wizard');
}
}

}
92 changes: 92 additions & 0 deletions src/PccSiteViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Drupal\pcx_connect;

/**
* Helper methods for adding custom behavior to the view.
*/
class PccSiteViewHelper {
/**
* Caching the config prefix.
*
* @var array
*/
protected static $configPrefixCache = [];

/**
* Retrieves mapping for Config Entity.
*
* @param string $id
* Config Entity id.
*
* @return array
* Returns array of Entity attributes.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function getMapping(string $id): ?array {
$config_type_all = self::getConfigTypedDefinitions();
$config_prefix = self::getConfigPrefix($id);

foreach ($config_type_all as $key => $value) {
if (isset($value['type']) && $value['type'] == 'config_entity') {
if (strpos($key, $config_prefix) === 0) {
return ($value['mapping'] ?? NULL);
}
}
}

// Return an empty array to avoid PHP errors when field definitions.
// That haven't been loaded yet or there is no mapping for the config
// entity.
return [];
}

/**
* Gets provider and config prefix for Config Entity.
*
* @param string $id
* Config Entity id.
*
* @return string
* Returns config prefix.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected static function getConfigPrefix(string $id): string {
if (isset(self::$configPrefixCache[$id])) {
return self::$configPrefixCache[$id];
}
$configPrefix = \Drupal::entityTypeManager()->getDefinition($id)->getConfigPrefix();
self::$configPrefixCache[$id] = $configPrefix;
return $configPrefix;
}

/**
* Gets the config.typed service.
*
* @return mixed
* Definitions of config.typed service.
*/
protected static function getConfigTypedDefinitions() {
return \Drupal::service('config.typed')->getDefinitions();
}

/**
* Gets the Config Entity Label.
*
* @param string $id
* Config Entity iD.
*
* @return string
* Config Entity label used in the group list.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function getConfigLabel(string $id): string {
return (string) \Drupal::entityTypeManager()
->getDefinition($id)
->getLabel();
}

}
Loading