Skip to main content

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 Fletcher17 Jan 2023
Drupal toc_js explode() deprecated function
A recent upgrade to PHP 8.1, has highlighted an error that hadn't been appearing previously. Deprecated function : explode(): Passing null to parameter #2 ($string) of type string is deprecated in toc_js_node_view()(line 130 of modules/contrib/toc_js/toc_js.module). In the top_js.module file...
Andrew Fletcher13 Jan 2023
Drupal twig look up to access field data
This guide has been compiled as a reference tool on how to access field values for different field types. The Manage Display admin UI can change how a field’s label and content are displayed. When I want to show how to output just the Manage Display version of the content, I use the word...