Entity View
Pinto Entity provides an alternative to using Drupal's Entity View Displays. Pinto Entity is analogous to using Panels, or Views for a single entity, but in a PHP code driven manner rather than mouse clicks and UI and persistence backed by configuration and YAML.
You'll start this guide with a ready-made Drupal installation, with Pinto configured. Node will also be used as the exemplary entity type, though any content entity type may be used.
This guide will also assist in modifying a pre-existing Pinto component. Follow Quick Start for a fast way of setting up Pinto and a simple component.
Entry point on component
The simplest setup is by providing the entry point on a component class itself.
Enable modules
Install and enable Drupal modules.
composer require drupal/pinto_entity
drush pm:install pinto_entityCreate a component class
Create a component to render out the entity. Here, we'll use the component from Quick Start. This assumes you also have the other required infrastructure, such as the Twig template.
namespace Drupal\my_site_ds;
use Drupal\pinto\Object\DrupalInvokableSlotsTrait;
class MyComponent {
use DrupalInvokableSlotsTrait;
public function __construct(
public string $str,
public int $number,
) {
}
}Add the entry point in the Pinto component
Modify the basic Pinto component with mappings for the entity type, bundle, and view mode.
namespace Drupal\my_site_ds;
use Drupal\pinto\Object\DrupalInvokableSlotsTrait;
use Drupal\pinto_entity\EntityView\Attribute\EntityView;
class MyComponent {
use DrupalInvokableSlotsTrait;
public function __construct(
public string $str,
public int $number,
) {
}
#[EntityView(entityType: 'node', bundles: 'bundleMachineName', viewModes: 'full')]
final public static function full(\Drupal\my_module\Entity\MyBundle $entity): static {
return new static(
str: (string) $entity->label(),
number: (string) $entity->id(),
)();
}
}Now, when an entity of type MyBundle is viewed and rendered, the method will be called, allowing you to fully specify the HTML, CSS, and JavaScript, via Pinto, bypassing cores' entity view displays.
Entry point on bundle class
Follow the component setup steps above, other than the entry point step.
Enable modules
Install and enable Drupal modules.
composer require drupal/bca
drush pm:install bcaSet up bundle classes
Bundle classes are supported by all maintained versions of Drupal. They allow adding custom PHP behavior to bundles, where a class represents one or more bundles.
What are bundles?
A bundle is a sub-type of an entity, for example node has node types, media have media types, terms have taxonomy etc.
The most straightforward way to set up bundle classes is by installing BCA.
Create a class in a custom module, under the Entity namespace:
namespace Drupal\my_module\Entity;
use Drupal\bca\Attribute\Bundle;
use Drupal\node\Entity\Node;
#[Bundle(entityType: 'node', bundle: 'bundleMachineName', label: new TranslatableMarkup('MyBundle'))]
final class MyBundle extends Node {}Replace bundleMachineName with the machine name of a bundle.
Replace MyBundle with a relevant class name and label.
Alterative: Core only, without BCA module.
Alternatively, to set up bundle class association without BCA, only core.
/**
* Implements hook_entity_bundle_info_alter().
*/
function my_module_entity_bundle_info_alter(array &$bundles): void {
$bundles['ENTITY_TYPE']['bundleMachineName']['class'] = \Drupal\my_module\Entity\MyBundle::class;
}The hook_entity_bundle_info_alter hook is used instead of #[Bundle] attribute
Add the entry point in the bundle class
Adding entity type, bundle, view mode to #[EntityView] is optional. They default to the entity type and bundle of the bundle class, and 'full' for the view mode.
namespace Drupal\my_module\Entity;
use Drupal\bca\Attribute\Bundle;
use Drupal\node\Entity\Node;
use Drupal\my_site_ds\MyComponent;
use Drupal\pinto_entity\EntityView\Attribute\EntityView;
#[Bundle(entityType: 'node', bundle: 'bundleMachineName', label: new TranslatableMarkup('MyBundle'))]
final class MyBundle extends Node {
#[EntityView]
final public static function full(self $entity): MyComponent {
return new MyComponent(
str: (string) $entity->label(),
number: (string) $entity->id(),
)();
}
}