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
•
09 Jan 2026
Upgrading Drupal from 10.6.x to 11.3.2: a practical, dependency-driven walkthrough
Upgrading from Drupal 10.6.x to 11.3.x is officially supported, but in real projects it’s rarely a single command. The friction usually comes from **Composer constraints**, not Drupal itself.This article documents a real-world upgrade path from Drupal 10.6.1 → 11.3.2, including the specific blockers...
Andrew Fletcher
•
04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher
•
26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...