Andrew Fletcher published: 9 June 2022 3 minutes read
This code is from Drupal 9 back-end for a React front-end via REST API.
Working through the output for recently viewed pages... the code structure:
<?php
namespace Drupal\custom\Plugin\GetData\paragraph;
use Drupal\custom\Plugin\GetData\ContentBase;
use Drupal\custom\Plugin\GetData\ParagraphTrait;
use Drupal\node\Entity\Node;
/**
* Provides Recent view pages data.
*
* @GetData(
* id = "recent_view_pages",
* title = @Translation("Recent view pages Data"),
* )
*/
class RecentViewPages extends ContentBase
{
use ParagraphTrait;
/**
* {@inheritdoc}
*/
public function process()
{
$this->processConfig();
$current_user = \Drupal::currentUser();
$nids = \Drupal::service('user.data')->get('custom', $current_user->id(), 'recent_views');
$nids = array_slice($nids, 0, 10);
$data = [];
foreach ($nids as $nid) {
$node = Node::load($nid);
// Confirm that the variable $node is an object, otherwise when NULL due to
// content access an error page is shown.
if (is_object($node)) {
$icon = $this->getIcons($node);
$data[] = [
'label' => $node->label(),
'icon' => $icon,
'url' => '/node/' . $nid
];
}
}
$this->setData('list', $data);
}
/**
* Get the icon for recently viewed pages.
*
* @param object $node
*
* @return string
*/
public function getIcons($node)
{
$type = strtolower($node->get('type')->target_id);
$name = $type;
// Target the document type.
if ($type == 'document') {
$term = $this->helper->getTaxonomyTermLabel($node, 'field_document_type');
$title = $this->helper->stripsNonAlphaNumeric($term);
$name = $this->cleanName($title);
}
$icon = $this->getReplacementNames($name);
return $icon;
}
/**
* Clean and prepare the string.
*
* @param string $str
*
* @return string
*/
public function cleanName($str)
{
$strLower = strtolower($str);
$name = str_replace([" ", " "], "_", $strLower);
return $name;
}
/**
* Convert the type name to the respective icon file name.
*
* @param string $name
*
* @return string
*/
public function getReplacementNames($name)
{
// Can be either content or document types.
$types = [
'content_page' => 'template',
'how_to_guide' => 'How-to',
'fact_sheet' => 'FactSheet',
'form' => 'Form',
'meeting_resources' => 'Meeting_Resources',
'news' => 'News',
'policy' => 'Policy',
'template_page' => 'template'
];
return $types[$name];
}
}
An example of the JSON output derived from this code
{
"recent_view_pages": {
"list": [
{
"label": "Branding Guidelines ",
"icon": "How-to",
"url": "/node/123"
},
{
"label": "Public Health Association Aboriginal and Torres Strait Islander guide to terminology",
"icon": "How-to",
"url": "/node/213"
},
{
"label": "Annual Business Planning Approach",
"icon": "How-to",
"url": "/node/1231"
},
{
"label": "Avaya one-X - User manual ",
"icon": "How-to",
"url": "/node/765"
},
{
"label": "Share an update",
"icon": "template",
"url": "/node/278"
},
{
"label": "How to and guides",
"icon": "template",
"url": "/node/946"
},
{
"label": "Work resources",
"icon": "template",
"url": "/node/811"
},
{
"label": "Fact sheets",
"icon": "template",
"url": "/node/177"
},
{
"label": "Templates",
"icon": "template",
"url": "/node/625"
}
]
}
}
Adjustments, currently the icon is the file name. Which can be either a SVG or PNG file. Additionally, it is not showing the directory path to the file. However, this can be easily updated to
{
"recent_view_pages": {
"list": [
{
"label": "Branding Guidelines ",
"icon": "sites/default/files/icons/How-to.svg",
"url": "/node/123"
},
// ..... //
]
}
}
Related articles
Andrew Fletcher
•
26 Apr 2024
TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string
I'm getting the following errorTypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string in Drupal\views\Plugin\views\argument\ArgumentPluginBase->unpackArgumentValue() (line 1302 of...
Andrew Fletcher
•
22 Apr 2024
Streamlined Drupal 10 Deployment: Best Practices for Stability and Security
Have you ever walked into a situation and marveled at how a site is still operational? Deployments that are riddled with fail-safes piled atop a shaky foundation. The real challenge lies in gradually persuading the team to embrace change to build a stable, robust deployment process aligned with...
Andrew Fletcher
•
21 Apr 2024
This command will help you spot any unusually large directories
The red flag was when I saw the server disk space is showing a site is taking up 57992.5 MB, where locally the site size is showing 957MB. There is something serious happening here and I need to establish the problem quickly. When dealing with a significant discrepancy in site size...