Skip to content

Commit

Permalink
PCC-76: Metadata field integration with views. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvir249 authored Jun 17, 2024
1 parent e5a15f8 commit 94df6b7
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 19 deletions.
26 changes: 8 additions & 18 deletions pcx_connect.views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ function pcx_connect_views_data() {
'id' => 'pcc_content',
'click sortable' => FALSE,
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];

$table['publishedDate'] = [
Expand All @@ -121,15 +115,9 @@ function pcx_connect_views_data() {
'id' => 'date',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];

$table['updatedAt'] = [
Expand All @@ -139,15 +127,9 @@ function pcx_connect_views_data() {
'id' => 'date',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];

$table['slug'] = [
Expand Down Expand Up @@ -194,6 +176,14 @@ function pcx_connect_views_data() {
'id' => 'string',
],
];

$table['metadata'] = [
'title' => $translation->translate('Metadata'),
'help' => $translation->translate('PCC Doc Metadata'),
'field' => [
'id' => 'pcc_metadata',
],
];
}
}
return array_filter($data);
Expand Down
17 changes: 17 additions & 0 deletions src/Pcc/Service/PccArticlesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PccPhpSdk\api\Query\Enums\ContentType;
use PccPhpSdk\api\Query\Enums\PublishingLevel;
use PccPhpSdk\api\Query\Enums\PublishStatus;
use PccPhpSdk\api\SitesApi;
use PccPhpSdk\Exception\PccClientException;

/**
Expand Down Expand Up @@ -158,4 +159,20 @@ protected function getArticlesApi(string $siteId, string $siteToken): ArticlesAp
return $this->articlesApi;
}

/**
* {@inheritDoc}
*/
public function getPccSiteData(string $siteId, string $siteToken): mixed {
$site_data = [];
try {
$api_client = $this->pccApiClient->getPccClient($siteId, $siteToken);
$contentApi = new SitesApi($api_client);
$site_data = $contentApi->getSite($siteId);
}
catch (PccClientException $e) {
$this->logger->error('Failed to get site data: <pre>' . print_r($e->getMessage(), TRUE) . '</pre>');
}
return $site_data;
}

}
13 changes: 13 additions & 0 deletions src/Pcc/Service/PccArticlesApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public function getArticle(
PublishingLevel $publishingLevel = PublishingLevel::PRODUCTION,
): mixed;

/**
* Get site data based on site id and site token.
*
* @param string $siteId
* Site ID.
* @param string $siteToken
* Site Token.
*
* @return mixed
* Returns an site data.
*/
public function getPccSiteData(string $siteId, string $siteToken): mixed;

}
138 changes: 138 additions & 0 deletions src/Plugin/views/field/PccMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Drupal\pcx_connect\Plugin\views\field;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\pcx_connect\Entity\PccSite;
use Drupal\pcx_connect\Pcc\Service\PccArticlesApiInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Handler to render html markup.
*
* @ingroup views_field_handlers
*
* @ViewsField("pcc_metadata")
*/
class PccMetadata extends FieldPluginBase {
/**
* PCC Content API service.
*
* @var \Drupal\pcx_connect\Pcc\Service\PccArticlesApiInterface
*/
protected PccArticlesApiInterface $pccContentApi;

/**
* Constructs a PccSiteViewQuery object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The database-specific date handler.
* @param \Drupal\pcx_connect\Pcc\Service\PccArticlesApiInterface $pccContentApi
* The PCC Content API Service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PccArticlesApiInterface $pccContentApi) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->pccContentApi = $pccContentApi;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('pcx_connect.pcc_articles_api'),
);
}

/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['pcc_metadata_fields'] = ['default' => ''];
return $options;
}

/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['pcc_metadata'] = [
'#type' => 'details',
'#title' => $this->t('PCC Metadata'),
'#weight' => 98,
];
$pcc_site = PccSite::load($this->table);
$options = [];
if ($pcc_site) {
$site_key = $pcc_site->get('site_key');
$site_token = $pcc_site->get('site_token');
$site_data = $this->pccContentApi->getPccSiteData($site_key, $site_token);
if ($site_data) {
$site_data_arr = Json::decode($site_data);
if (!empty($site_data_arr['data']['site']['metadataFields'])) {
$metadata = $site_data_arr['data']['site']['metadataFields'];
foreach ($metadata as $data) {
$options[$data['title']] = $this->t('@title', ['@title' => $data['title']]);
}
}
}
if ($options) {
$form['pcc_metadata_fields'] = [
'#type' => 'radios',
'#title' => $this->t('Available Metadata Fields'),
'#options' => $options,
'#default_value' => $this->options['pcc_metadata_fields'],
'#fieldset' => 'pcc_metadata',
];
}
else {
$form['pcc_metadata_empty'] = [
'#type' => 'markup',
'#markup' => 'Metadata fields are not available for this site.',
'#fieldset' => 'pcc_metadata',
];
}
}
parent::buildOptionsForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
if (!$form_state->getValue(['options', 'pcc_metadata_fields'])) {
$form_state->setError($form['pcc_metadata'], $this->t('Metadata fields are not available for this site.'));
}
}

/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$metadata_field = $this->options['pcc_metadata_fields'];
$value = $this->getValue($values);

if ($value && isset($value[$metadata_field])) {
// Post Date returns an array so need special handeling.
if ($metadata_field === 'Post Date') {
$field_value = $value[$metadata_field]['msSinceEpoch'];
}
else {
$field_value = $value[$metadata_field];
}
return $field_value;
}
}

}
2 changes: 1 addition & 1 deletion src/Plugin/views/query/PccSiteViewQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ protected function getArticlesFromPccContentApi(ViewExecutable &$view): void {
$field_keys = array_keys($this->fields);

$articles = $this->pccContentApi->getAllArticles($this->siteKey, $this->siteToken, $field_keys, $pager);

$index = 0;
if ($articles) {

foreach ($articles['articles'] as $article) {
// Render articles based on pager.
$view->result[] = $this->toRow($article, $index++);
Expand Down

0 comments on commit 94df6b7

Please sign in to comment.