Initial Drupal 11 with DDEV setup

This commit is contained in:
gluebox
2025-10-08 11:39:17 -04:00
commit 89ef74b305
25344 changed files with 2599172 additions and 0 deletions

View File

@ -0,0 +1,47 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Command;
{% apply sort_namespaces %}
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
// phpcs:disable Drupal.Commenting.ClassComment.Missing
#[AsCommand(
name: '{{ command.name }}',
description: '{{ command.description }}',
aliases: ['{{ command.alias }}'],
)]
final class {{ class }} extends Command {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {
{# Parent constructor configures the command. #}
parent::__construct();
}
{% endif %}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
// @todo Place your code here.
$output->writeln('<info>It works!</info>');
return self::SUCCESS;
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
{{ service_name }}:
class: Drupal\{{ machine_name }}\Command\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: console.command }

View File

@ -0,0 +1,14 @@
{{ machine_name }}.{{ entity_type_id }}.*:
type: config_entity
label: {{ entity_type_label }}
mapping:
id:
type: string
label: ID
label:
type: label
label: Label
uuid:
type: string
description:
type: string

View File

@ -0,0 +1,5 @@
entity.{{ entity_type_id }}.add_form:
route_name: 'entity.{{ entity_type_id }}.add_form'
title: 'Add {{ entity_type_label|lower }}'
appears_on:
- entity.{{ entity_type_id }}.collection

View File

@ -0,0 +1,5 @@
entity.{{ entity_type_id }}.overview:
title: {{ entity_type_label|pluralize }}
parent: system.admin_structure
description: 'List of {{ entity_type_label|lower|pluralize }} to extend site functionality.'
route_name: entity.{{ entity_type_id }}.collection

View File

@ -0,0 +1,2 @@
administer {{ entity_type_id }}:
title: 'Administer {{ entity_type_label|lower }}'

View File

@ -0,0 +1,31 @@
entity.{{ entity_type_id }}.collection:
path: '/admin/structure/{{ entity_type_id|u2h }}'
defaults:
_entity_list: '{{ entity_type_id }}'
_title: '{{ entity_type_label }} configuration'
requirements:
_permission: 'administer {{ entity_type_id }}'
entity.{{ entity_type_id }}.add_form:
path: '/admin/structure/{{ entity_type_id }}/add'
defaults:
_entity_form: '{{ entity_type_id }}.add'
_title: 'Add {{ entity_type_label|article|lower }}'
requirements:
_permission: 'administer {{ entity_type_id }}'
entity.{{ entity_type_id }}.edit_form:
path: '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}'
defaults:
_entity_form: '{{ entity_type_id }}.edit'
_title: 'Edit {{ entity_type_label|article|lower }}'
requirements:
_permission: 'administer {{ entity_type_id }}'
entity.{{ entity_type_id }}.delete_form:
path: '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete'
defaults:
_entity_form: '{{ entity_type_id }}.delete'
_title: 'Delete {{ entity_type_label|article|lower }}'
requirements:
_permission: 'administer {{ entity_type_id }}'

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Entity;
{% apply sort_namespaces %}
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
use Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder;
use Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form;
{% endapply %}
/**
* Defines the {{ entity_type_label|lower }} entity type.
*/
#[ConfigEntityType(
id: '{{ entity_type_id }}',
label: new TranslatableMarkup('{{ entity_type_label }}'),
label_collection: new TranslatableMarkup('{{ entity_type_label|pluralize }}'),
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }}'),
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }}'),
config_prefix: '{{ entity_type_id }}',
entity_keys: [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class_prefix }}ListBuilder::class,
'form' => [
'add' => {{ class_prefix }}Form::class,
'edit' => {{ class_prefix }}Form::class,
'delete' => EntityDeleteForm::class,
],
],
links: [
'collection' => '/admin/structure/{{ entity_type_id|u2h }}',
'add-form' => '/admin/structure/{{ entity_type_id|u2h }}/add',
'edit-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
'delete-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
],
admin_permission: 'administer {{ entity_type_id }}',
label_count: [
'singular' => '@count {{ entity_type_label|lower }}',
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
],
config_export: [
'id',
'label',
'description',
],
)]
final class {{ class_prefix }} extends ConfigEntityBase implements {{ class_prefix }}Interface {
/**
* The example ID.
*/
protected string $id;
/**
* The example label.
*/
protected string $label;
/**
* The example description.
*/
protected string $description;
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining {{ entity_type_label|article|lower }} entity type.
*/
interface {{ class_prefix }}Interface extends ConfigEntityInterface {
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of {{ entity_type_label|lower|pluralize }}.
*/
final class {{ class_prefix }}ListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader(): array {
$header['label'] = $this->t('Label');
$header['id'] = $this->t('Machine name');
$header['status'] = $this->t('Status');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity): array {
/** @var \Drupal\{{ machine_name }}\{{ class_prefix }}Interface $entity */
$row['label'] = $entity->label();
$row['id'] = $entity->id();
$row['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
return $row + parent::buildRow($entity);
}
}

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\{{ machine_name }}\Entity\{{ class_prefix }};
/**
* {{ entity_type_label }} form.
*/
final class {{ class_prefix }}Form extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state): array {
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity->id(),
'#machine_name' => [
'exists' => [{{ class_prefix }}::class, 'load'],
],
'#disabled' => !$this->entity->isNew(),
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
'#default_value' => $this->entity->status(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this->t('Description'),
'#default_value' => $this->entity->get('description'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state): int {
$result = parent::save($form, $form_state);
$message_args = ['%label' => $this->entity->label()];
$this->messenger()->addStatus(
match($result) {
\SAVED_NEW => $this->t('Created new example %label.', $message_args),
\SAVED_UPDATED => $this->t('Updated example %label.', $message_args),
}
);
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
return $result;
}
}

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
module:
- {{ machine_name }}
id: {{ entity_type_id }}_delete_action
label: 'Delete {{ entity_type_label|pluralize|lower }}'
type: {{ entity_type_id }}
plugin: entity:delete_action:{{ entity_type_id }}
configuration: { }

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
module:
- {{ machine_name }}
id: {{ entity_type_id }}_save_action
label: 'Save {{ entity_type_label|pluralize|lower }}'
type: {{ entity_type_id }}
plugin: entity:save_action:{{ entity_type_id }}
configuration: { }

View File

@ -0,0 +1,31 @@
id: entity.{{ entity_type_id }}
plugin_id: 'entity:{{ entity_type_id }}'
granularity: method
configuration:
GET:
supported_formats:
- json
- xml
supported_auth:
- cookie
POST:
supported_formats:
- json
- xml
supported_auth:
- cookie
PATCH:
supported_formats:
- json
- xml
supported_auth:
- cookie
DELETE:
supported_formats:
- json
- xml
supported_auth:
- cookie
dependencies:
module:
- user

View File

@ -0,0 +1,12 @@
{{ machine_name }}.{{ entity_type_id }}_type.*:
type: config_entity
label: '{{ entity_type_label }} type config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string

View File

@ -0,0 +1,19 @@
{% if bundle %}
{{ entity_type_id }}.type_add:
title: 'Add {{ entity_type_label|lower }} type'
route_name: entity.{{ entity_type_id }}_type.add_form
appears_on:
- entity.{{ entity_type_id }}_type.collection
{{ entity_type_id }}.add_page:
title: 'Add {{ entity_type_label|lower }}'
route_name: entity.{{ entity_type_id }}.add_page
appears_on:
- entity.{{ entity_type_id }}.collection
{% else %}
{{ entity_type_id }}.add_form:
title: 'Add {{ entity_type_label|lower }}'
route_name: entity.{{ entity_type_id }}.add_form
appears_on:
- entity.{{ entity_type_id }}.collection
{% endif %}

View File

@ -0,0 +1,10 @@
entity.{{ entity_type_id }}.edit_form:
route_name: entity.{{ entity_type_id }}.edit_form
group: {{ entity_type_id }}
title: 'Edit'
entity.{{ entity_type_id }}.delete_form:
route_name: entity.{{ entity_type_id }}.delete_form
group: {{ entity_type_id }}
title: 'Delete'
weight: 10

View File

@ -0,0 +1,20 @@
{% if fieldable_no_bundle %}
entity.{{ entity_type_id }}.settings:
title: '{{ entity_type_label }}'
description: 'Configure {{ entity_type_label|article }} entity type.'
route_name: entity.{{ entity_type_id }}.settings
parent: system.admin_structure
{% elseif bundle %}
entity.{{ entity_type_id }}_type.collection:
title: '{{ entity_type_label }} types'
description: 'Manage and CRUD actions on {{ entity_type_label }} type.'
parent: system.admin_structure
route_name: entity.{{ entity_type_id }}_type.collection
{% endif %}
entity.{{ entity_type_id }}.collection:
title: '{{ entity_type_label|pluralize }}'
description: 'List of {{ entity_type_label|pluralize|lower }}.'
route_name: entity.{{ entity_type_id }}.collection
parent: system.admin_content

View File

@ -0,0 +1,37 @@
{% if fieldable_no_bundle %}
entity.{{ entity_type_id }}.settings:
title: 'Settings'
route_name: entity.{{ entity_type_id }}.settings
base_route: entity.{{ entity_type_id }}.settings
{% endif %}
{# Tabs are not needed when there is no canonical view page. #}
{% if canonical %}
entity.{{ entity_type_id }}.view:
title: 'View'
route_name: entity.{{ entity_type_id }}.canonical
base_route: entity.{{ entity_type_id }}.canonical
entity.{{ entity_type_id }}.edit_form:
title: 'Edit'
route_name: entity.{{ entity_type_id }}.edit_form
base_route: entity.{{ entity_type_id }}.canonical
entity.{{ entity_type_id }}.delete_form:
title: 'Delete'
route_name: entity.{{ entity_type_id }}.delete_form
base_route: entity.{{ entity_type_id }}.canonical
weight: 10
{% endif %}
entity.{{ entity_type_id }}.collection:
title: '{{ entity_type_label|pluralize }}'
route_name: entity.{{ entity_type_id }}.collection
base_route: system.admin_content
weight: 10
{% if bundle %}
entity.{{ entity_type_id }}_type.edit_form:
title: 'Edit'
route_name: entity.{{ entity_type_id }}_type.edit_form
base_route: entity.{{ entity_type_id }}_type.edit_form
entity.{{ entity_type_id }}_type.collection:
title: 'List'
route_name: entity.{{ entity_type_id }}_type.collection
base_route: entity.{{ entity_type_id }}_type.collection
{% endif %}

View File

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
/**
* @file
* Provides {{ entity_type_label|article|lower }} entity type.
*/
{% apply sort_namespaces %}
use Drupal\Core\Render\Element;
{% if author_base_field %}
use Drupal\user\UserInterface;
{% endif %}
{% endapply %}
/**
* Implements hook_theme().
*/
function {{ machine_name }}_theme(): array {
return [
'{{ entity_type_id }}' => ['render element' => 'elements'],
];
}
/**
* Prepares variables for {{ entity_type_label|lower }} templates.
*
* Default template: {{ template_name }}.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the {{ entity_type_label|lower }} information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_{{ entity_type_id }}(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
{% if author_base_field %}
/**
* Implements hook_user_cancel().
*/
function {{ machine_name }}_user_cancel($edit, UserInterface $account, $method): void {
switch ($method) {
{% if status_base_field %}
case 'user_cancel_block_unpublish':
// Unpublish {{ entity_type_label|lower|pluralize }}.
$storage = \Drupal::entityTypeManager()->getStorage('{{ entity_type_id }}');
${{ entity_type_id }}_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple(${{ entity_type_id }}_ids) as ${{ entity_type_id }}) {
${{ entity_type_id }}->set('status', FALSE)->save();
}
break;
{% endif %}
case 'user_cancel_reassign':
// Anonymize {{ entity_type_label|lower|pluralize }}.
$storage = \Drupal::entityTypeManager()->getStorage('{{ entity_type_id }}');
${{ entity_type_id }}_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple(${{ entity_type_id }}_ids) as ${{ entity_type_id }}) {
${{ entity_type_id }}->setOwnerId(0)->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function {{ machine_name }}_user_predelete(UserInterface $account): void {
// Delete {{ entity_type_label|lower|pluralize }} that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('{{ entity_type_id }}');
${{ entity_type_id }}_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple(${{ entity_type_id }}_ids)
);
{% if revisionable %}
// Delete old revisions.
${{ entity_type_id }}_ids = $storage->getQuery()
->allRevisions()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach (array_keys(${{ entity_type_id }}_ids) as $revision_id) {
$storage->deleteRevision($revision_id);
}
{% endif %}
}
{% endif %}

View File

@ -0,0 +1,26 @@
{{ permissions.administer }}:
{% if bundle %}
title: 'Administer {{ entity_type_label|lower }} types'
description: 'Maintain the types of {{ entity_type_id_short|replace({'_': ' '}) }} entity.'
{% else %}
title: 'Administer {{ entity_type_label|lower|pluralize }}'
{% endif %}
restrict access: true
{% if access_controller %}
{{ permissions.view }}:
title: 'View {{ entity_type_label|lower }}'
{{ permissions.edit }}:
title: 'Edit {{ entity_type_label|lower }}'
{{ permissions.delete }}:
title: 'Delete {{ entity_type_label|lower }}'
{{ permissions.create }}:
title: 'Create {{ entity_type_label|lower }}'
{% endif %}
{% if revisionable and access_controller %}
{{ permissions.view_revision }}:
title: 'View {{ entity_type_label|lower }} revision'
{{ permissions.revert_revision }}:
title: 'Revert {{ entity_type_label|lower }} revision'
{{ permissions.delete_revision }}:
title: 'Delete {{ entity_type_label|lower }} revision'
{% endif %}

View File

@ -0,0 +1,9 @@
{% if fieldable_no_bundle %}
entity.{{ entity_type_id }}.settings:
path: 'admin/structure/{{ entity_type_id|u2h }}'
defaults:
_form: '\Drupal\{{ machine_name }}\Form\{{ class }}SettingsForm'
_title: '{{ entity_type_label }}'
requirements:
_permission: '{{ permissions.administer }}'
{% endif %}

View File

@ -0,0 +1,334 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Entity;
{% apply sort_namespaces %}
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\views\EntityViewsData;
use Drupal\Core\Entity\Form\DeleteMultipleForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\{{ class }}ListBuilder;
use Drupal\{{ machine_name }}\Form\{{ class }}Form;
{% if author_base_field %}
use Drupal\Core\Entity\EntityStorageInterface;
{% endif %}
{% if has_base_fields %}
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
{% endif %}
{% if not revisionable %}
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
{% if revisionable %}
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\Form\RevisionDeleteForm;
use Drupal\Core\Entity\Form\RevisionRevertForm;
use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider;
{% endif %}
use Drupal\{{ machine_name }}\{{ class }}Interface;
{% if author_base_field %}
use Drupal\user\EntityOwnerTrait;
{% endif %}
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedTrait;
{% endif %}
{% if canonical %}
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
{% else %}
use Drupal\{{ machine_name }}\Routing\{{ class }}HtmlRouteProvider;
{% endif %}
{% if access_controller %}
use Drupal\{{ machine_name }}\{{ class }}AccessControlHandler;
{% endif %}
{% endapply %}
/**
* Defines the {{ entity_type_label|lower }} entity class.
*/
#[ContentEntityType(
id: '{{ entity_type_id }}',
label: new TranslatableMarkup('{{ entity_type_label }}'),
label_collection: new TranslatableMarkup('{{ entity_type_label|pluralize }}'),
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }}'),
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }}'),
entity_keys: [
'id' => 'id',
{% if revisionable %}
'revision' => 'revision_id',
{% endif %}
{% if translatable %}
'langcode' => 'langcode',
{% endif %}
{% if bundle %}
'bundle' => 'bundle',
{% endif %}
'label' => '{{ label_base_field ? 'label' : 'id' }}',
{% if author_base_field %}
'owner' => 'uid',
{% endif %}
{% if status_base_field %}
'published' => 'status',
{% endif %}
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class }}ListBuilder::class,
'views_data' => EntityViewsData::class,
{% if access_controller %}
'access' => {{ class }}AccessControlHandler::class,
{% endif %}
'form' => [
'add' => {{ class }}Form::class,
'edit' => {{ class }}Form::class,
'delete' => ContentEntityDeleteForm::class,
'delete-multiple-confirm' => DeleteMultipleForm::class,
{% if revisionable %}
'revision-delete' => RevisionDeleteForm::class,
'revision-revert' => RevisionRevertForm::class,
{% endif %}
],
'route_provider' => [
{% if canonical %}
'html' => AdminHtmlRouteProvider::class,
{% else %}
'html' => {{ class }}HtmlRouteProvider::class,
{% endif %}
{% if revisionable %}
'revision' => RevisionHtmlRouteProvider::class,
{% endif %}
],
],
links: [
'collection' => '/admin/content/{{ entity_type_id_short|u2h }}',
{% if bundle %}
'add-form' => '{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}',
'add-page' => '{{ entity_base_path }}/add',
{% else %}
'add-form' => '{{ entity_base_path }}/add',
{% endif %}
'canonical' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
{% if canonical %}
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit',
{% else %}
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
{% endif %}
'delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
'delete-multiple-form' => '/admin/content/{{ entity_type_id_short|u2h }}/delete-multiple',
{% if revisionable %}
'revision' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/view',
'revision-delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/delete',
'revision-revert-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/revert',
'version-history' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revisions',
{% endif %}
],
admin_permission: '{{ permissions.administer }}',
{% if bundle %}
bundle_entity_type: '{{ entity_type_id }}_type',
bundle_label: new TranslatableMarkup('{{ entity_type_label }} type'),
{% endif %}
base_table: '{{ entity_type_id }}',
{% if translatable %}
data_table: '{{ entity_type_id }}_field_data',
{% endif %}
{% if revisionable %}
revision_table: '{{ entity_type_id }}_revision',
{% endif %}
{% if revisionable and translatable %}
revision_data_table: '{{ entity_type_id }}_field_revision',
{% endif %}
{% if translatable %}
translatable: TRUE,
{% endif %}
{% if revisionable %}
show_revision_ui: TRUE,
{% endif %}
label_count: [
'singular' => '@count {{ entity_type_label|pluralize|lower }}',
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
],
{% if bundle %}
field_ui_base_route: 'entity.{{ entity_type_id }}_type.edit_form',
{% elseif fieldable %}
field_ui_base_route: 'entity.{{ entity_type_id }}.settings',
{% endif %}
{% if revisionable %}
revision_metadata_keys: [
'revision_user' => 'revision_uid',
'revision_created' => 'revision_timestamp',
'revision_log_message' => 'revision_log',
],
{% endif %}
)]
class {{ class }} extends {% if revisionable %}Editorial{% endif %}ContentEntityBase implements {{ class }}Interface {
{% if changed_base_field or author_base_field %}
{% if changed_base_field %}
use EntityChangedTrait;
{% endif %}
{# use EntityCreatedTrait once it is added to Drupal core #}
{# @see https://www.drupal.org/node/2833378 #}
{% if author_base_field %}
use EntityOwnerTrait;
{% endif %}
{% endif %}
{% if author_base_field %}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage): void {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
}
{% endif %}
{% if has_base_fields %}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
$fields = parent::baseFieldDefinitions($entity_type);
{% if label_base_field %}
$fields['label'] = BaseFieldDefinition::create('string')
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setLabel(t('Label'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
{% endif %}
{% if status_base_field %}
$fields['status'] = BaseFieldDefinition::create('boolean')
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
->setLabel(t('Status'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'above',
'weight' => 0,
'settings' => [
'format' => 'enabled-disabled',
],
])
->setDisplayConfigurable('view', TRUE);
{% endif %}
{% if description_base_field %}
$fields['description'] = BaseFieldDefinition::create('text_long')
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setLabel(t('Description'))
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'text_default',
'label' => 'above',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
{% endif %}
{% if author_base_field %}
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setLabel(t('Author'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
{% endif %}
{% if created_base_field %}
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setDescription(t('The time that the {{ entity_type_label|lower }} was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
{% endif %}
{% if changed_base_field %}
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setDescription(t('The time that the {{ entity_type_label|lower }} was last edited.'));
{% endif %}
return $fields;
}
{% endif %}
}

View File

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Entity;
{% apply sort_namespaces %}
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\Form\{{ class }}TypeForm;
use Drupal\{{ machine_name }}\{{ class }}TypeListBuilder;
{% endapply %}
/**
* Defines the {{ entity_type_label }} type configuration entity.
*/
#[ConfigEntityType(
id: '{{ entity_type_id }}_type',
label: new TranslatableMarkup('{{ entity_type_label }} type'),
label_collection: new TranslatableMarkup('{{ entity_type_label }} types'),
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }} type'),
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }} types'),
config_prefix: '{{ entity_type_id }}_type',
entity_keys: [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class }}TypeListBuilder::class,
'route_provider' => [
'html' => AdminHtmlRouteProvider::class,
],
'form' => [
'add' => {{ class }}TypeForm::class,
'edit' => {{ class }}TypeForm::class,
'delete' => EntityDeleteForm::class,
],
],
links: [
'add-form' => '/admin/structure/{{ entity_type_id }}_types/add',
'edit-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}',
'delete-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}/delete',
'collection' => '/admin/structure/{{ entity_type_id }}_types',
],
admin_permission: '{{ permissions.administer }}',
bundle_of: '{{ entity_type_id }}',
label_count: [
'singular' => '@count {{ entity_type_label|lower }} type',
'plural' => '@count {{ entity_type_label|pluralize|lower }} types',
],
config_export: [
'id',
'label',
'uuid',
],
)]
final class {{ class }}Type extends ConfigEntityBundleBase {
/**
* The machine name of this {{ entity_type_label|lower }} type.
*/
protected string $id;
/**
* The human-readable name of the {{ entity_type_label|lower }} type.
*/
protected string $label;
}

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the access control handler for the {{ entity_type_label|lower }} entity type.
*
* phpcs:disable Drupal.Arrays.Array.LongLineDeclaration
*
* @see https://www.drupal.org/project/coder/issues/3185082
*/
final class {{ class }}AccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account): AccessResult {
if ($account->hasPermission($this->entityType->getAdminPermission())) {
return AccessResult::allowed()->cachePerPermissions();
}
return match($operation) {
'view' => AccessResult::allowedIfHasPermission($account, '{{ permissions.view }}'),
'update' => AccessResult::allowedIfHasPermission($account, '{{ permissions.edit }}'),
'delete' => AccessResult::allowedIfHasPermission($account, '{{ permissions.delete }}'),
{% if revisionable %}
'delete revision' => AccessResult::allowedIfHasPermission($account, '{{ permissions.delete_revision }}'),
'view all revisions', 'view revision' => AccessResult::allowedIfHasPermissions($account, ['{{ permissions.view_revision }}', '{{ permissions.view }}']),
'revert' => AccessResult::allowedIfHasPermissions($account, ['{{ permissions.revert_revision }}', '{{ permissions.edit }}']),
{% endif %}
default => AccessResult::neutral(),
};
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL): AccessResult {
return AccessResult::allowedIfHasPermissions($account, ['{{ permissions.create }}', '{{ permissions.administer }}'], 'OR');
}
}

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Entity\ContentEntityInterface;
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedInterface;
{% endif %}
{% if author_base_field %}
use Drupal\user\EntityOwnerInterface;
{% endif %}
/**
* Provides an interface defining {{ entity_type_label|article|lower }} entity type.
*/
interface {{ class }}Interface extends ContentEntityInterface{% if author_base_field %}, EntityOwnerInterface{% endif %}{% if changed_base_field %}, EntityChangedInterface{% endif %} {
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
/**
* Provides a list controller for the {{ entity_type_label|lower }} entity type.
*/
final class {{ class }}ListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader(): array {
$header['id'] = $this->t('ID');
{% if label_base_field %}
$header['label'] = $this->t('Label');
{% endif %}
{% if status_base_field %}
$header['status'] = $this->t('Status');
{% endif %}
{% if author_base_field %}
$header['uid'] = $this->t('Author');
{% endif %}
{% if created_base_field %}
$header['created'] = $this->t('Created');
{% endif %}
{% if changed_base_field %}
$header['changed'] = $this->t('Updated');
{% endif %}
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity): array {
/** @var \Drupal\{{ machine_name }}\{{ class }}Interface $entity */
$row['id'] = $entity->{{ label_base_field or not canonical ? 'id' : 'toLink' }}();
{% if label_base_field %}
$row['label'] = $entity->{{ canonical ? 'toLink' : 'label' }}();
{% endif %}
{% if status_base_field %}
$row['status'] = $entity->get('status')->value ? $this->t('Enabled') : $this->t('Disabled');
{% endif %}
{% if author_base_field %}
$username_options = [
'label' => 'hidden',
'settings' => ['link' => $entity->get('uid')->entity->isAuthenticated()],
];
$row['uid']['data'] = $entity->get('uid')->view($username_options);
{% endif %}
{% if created_base_field %}
$row['created']['data'] = $entity->get('created')->view(['label' => 'hidden']);
{% endif %}
{% if changed_base_field %}
$row['changed']['data'] = $entity->get('changed')->view(['label' => 'hidden']);
{% endif %}
return $row + parent::buildRow($entity);
}
}

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Defines a class to build a listing of {{ entity_type_label|lower }} type entities.
*
* @see \Drupal\{{ machine_name }}\Entity\{{ class }}Type
*/
final class {{ class }}TypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader(): array {
$header['label'] = $this->t('Label');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity): array {
$row['label'] = $entity->label();
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function render(): array {
$build = parent::render();
$build['table']['#empty'] = $this->t(
'No {{ entity_type_label|lower }} types available. <a href=":link">Add {{ entity_type_label|lower }} type</a>.',
[':link' => Url::fromRoute('entity.{{ entity_type_id }}_type.add_form')->toString()],
);
return $build;
}
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for the {{ entity_type_label|lower }} entity edit forms.
*/
final class {{ class }}Form extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state): int {
$result = parent::save($form, $form_state);
$message_args = ['%label' => $this->entity->toLink()->toString()];
$logger_args = [
'%label' => $this->entity->label(),
'link' => $this->entity->toLink($this->t('View'))->toString(),
];
switch ($result) {
case SAVED_NEW:
$this->messenger()->addStatus($this->t('New {{ entity_type_label|lower }} %label has been created.', $message_args));
$this->logger('{{ machine_name }}')->notice('New {{ entity_type_label|lower }} %label has been created.', $logger_args);
break;
case SAVED_UPDATED:
$this->messenger()->addStatus($this->t('The {{ entity_type_label|lower }} %label has been updated.', $message_args));
$this->logger('{{ machine_name }}')->notice('The {{ entity_type_label|lower }} %label has been updated.', $logger_args);
break;
default:
throw new \LogicException('Could not save the entity.');
}
{% if canonical %}
$form_state->setRedirectUrl($this->entity->toUrl());
{% else %}
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
{% endif %}
return $result;
}
}

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configuration form for {{ entity_type_label|article|lower }} entity type.
*/
final class {{ class }}SettingsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return '{{ entity_type_id }}_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['settings'] = [
'#markup' => $this->t('Settings form for {{ entity_type_label|article|lower }} entity type.'),
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Save'),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->messenger()->addStatus($this->t('The configuration has been updated.'));
}
}

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
{% apply sort_namespaces %}
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\{{ machine_name }}\Entity\{{ class }}Type;
{% endapply %}
/**
* Form handler for {{ entity_type_label|lower }} type forms.
*/
final class {{ class }}TypeForm extends BundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state): array {
$form = parent::form($form, $form_state);
if ($this->operation === 'edit') {
$form['#title'] = $this->t('Edit %label {{ entity_type_label|lower }} type', ['%label' => $this->entity->label()]);
}
$form['label'] = [
'#title' => $this->t('Label'),
'#type' => 'textfield',
'#default_value' => $this->entity->label(),
'#description' => $this->t('The human-readable name of this {{ entity_type_label|lower }} type.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => [{{ class }}Type::class, 'load'],
'source' => ['label'],
],
'#description' => $this->t('A unique machine-readable name for this {{ entity_type_label|lower }} type. It must only contain lowercase letters, numbers, and underscores.'),
];
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state): array {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save {{ entity_type_label|lower }} type');
$actions['delete']['#value'] = $this->t('Delete {{ entity_type_label|lower }} type');
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state): int {
$result = parent::save($form, $form_state);
$message_args = ['%label' => $this->entity->label()];
$this->messenger()->addStatus(
match($result) {
SAVED_NEW => $this->t('The {{ entity_type_label|lower }} type %label has been added.', $message_args),
SAVED_UPDATED => $this->t('The {{ entity_type_label|lower }} type %label has been updated.', $message_args),
}
);
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
return $result;
}
}

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Routing;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides HTML routes for entities with administrative pages.
*/
final class {{ class }}HtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
protected function getCanonicalRoute(EntityTypeInterface $entity_type): ?Route {
return $this->getEditFormRoute($entity_type);
}
}

View File

@ -0,0 +1,24 @@
{{ '{#' }}
/**
* @file
* Default theme implementation to present {{ entity_type_label|article|lower }} entity.
*
* This template is used when viewing a canonical {{ entity_type_label|lower }} page,
*
* Available variables:
* - content: A list of content items. Use 'content' to print all content, or
* print a subset such as 'content.label'.
* - attributes: HTML attributes for the container element.
*
* @see template_preprocess_{{ entity_type_id }}()
*/
{{ '#}' }}{% verbatim %}
<article{{ attributes }}>
{% if view_mode != 'full' %}
{{ title_prefix }}
{{ title_suffix }}
{% endif %}
{% if content %}
{{- content -}}
{% endif %}
</article>{% endverbatim %}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace {{ namespace }};
use {{ entity_class_fqn }};
/**
* A base bundle class for {{ entity_type_id }} entities.
*/
abstract class {{ base_class }} extends {{ entity_class }} {
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace {{ namespace }};
{% if not base_class %}
use {{ entity_class_fqn }};
{% endif %}
/**
* A bundle class for {{ entity_type_id }} entities.
*/
final class {{ class }} extends {{ base_class ?: entity_class }} {
}

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/**
* @file
* Primary module hooks for {{ name }} module.
*/
/**
* Implements hook_entity_bundle_info_alter().
*/
function {{ machine_name }}_entity_bundle_info_alter(array &$bundles): void {
{% for bundle_id, class_fqn in classes_fqn %}
if (isset($bundles['{{ entity_type_id }}']['{{ bundle_id }}'])) {
// phpcs:ignore Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing
$bundles['{{ entity_type_id }}']['{{ bundle_id }}']['class'] = {{ class_fqn }}::class;
}
{% endfor %}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure {{ name }} settings for this site.
*/
final class {{ class }} extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return '{{ form_id }}';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['{{ machine_name }}.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->config('{{ machine_name }}.settings')->get('example'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// @todo Validate the form here.
// Example:
// @code
// if ($form_state->getValue('example') === 'wrong') {
// $form_state->setErrorByName(
// 'message',
// $this->t('The value is not correct.'),
// );
// }
// @endcode
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('{{ machine_name }}.settings')
->set('example', $form_state->getValue('example'))
->save();
parent::submitForm($form, $form_state);
}
}

View File

@ -0,0 +1,10 @@
{{ route_name }}:
title: {{ link_title }}
{% if link_description %}
description: {{ link_description }}
{% endif %}
{% if link_parent %}
parent: {{ link_parent }}
{% endif %}
route_name: {{ route_name }}
weight: 10

View File

@ -0,0 +1,7 @@
{{ route_name }}:
path: '{{ route_path }}'
defaults:
_title: '{{ route_title }}'
_form: 'Drupal\{{ machine_name }}\Form\{{ class }}'
requirements:
_permission: '{{ route_permission }}'

View File

@ -0,0 +1,8 @@
# Schema for the configuration files of the {{ name }} module.
{{ machine_name }}.settings:
type: config_object
label: '{{ name }} settings'
mapping:
example:
type: string
label: 'Example'

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* @todo Add a description for the form.
*/
final class {{ class }} extends ConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return '{{ form_id }}';
}
/**
* {@inheritdoc}
*/
public function getQuestion(): TranslatableMarkup {
return $this->t('Are you sure you want to do this?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl(): Url {
return new Url('system.admin_config');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
// @todo Place your code here.
$this->messenger()->addStatus($this->t('Done!'));
$form_state->setRedirectUrl(new Url('system.admin_config'));
}
}

View File

@ -0,0 +1,7 @@
{{ route_name }}:
path: '{{ route_path }}'
defaults:
_title: '{{ route_title }}'
_form: 'Drupal\{{ machine_name }}\Form\{{ class }}'
requirements:
_permission: '{{ route_permission }}'

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a {{ name }} form.
*/
final class {{ class }} extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return '{{ form_id }}';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['message'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Send'),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// @todo Validate the form here.
// Example:
// @code
// if (mb_strlen($form_state->getValue('message')) < 10) {
// $form_state->setErrorByName(
// 'message',
// $this->t('Message should be at least 10 characters.'),
// );
// }
// @endcode
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->messenger()->addStatus($this->t('The message has been sent.'));
$form_state->setRedirect('<front>');
}
}

View File

@ -0,0 +1,7 @@
{{ route_name }}:
path: '{{ route_path }}'
defaults:
_title: '{{ route_title }}'
_form: 'Drupal\{{ machine_name }}\Form\{{ class }}'
requirements:
_permission: '{{ route_permission }}'

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;
{% apply sort_namespaces %}
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
{% endapply %}
/**
* Plugin implementation of the '{{ plugin_label }}' formatter.
*/
#[FieldFormatter(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
field_types: ['string'],
)]
class {{ class }} extends FormatterBase {
{% if configurable %}
/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
$setting = ['foo' => 'bar'];
return $setting + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$elements['foo'] = [
'#type' => 'textfield',
'#title' => $this->t('Foo'),
'#default_value' => $this->getSetting('foo'),
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary(): array {
return [
$this->t('Foo: @foo', ['@foo' => $this->getSetting('foo')]),
];
}
{% endif %}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode): array {
$element = [];
foreach ($items as $delta => $item) {
$element[$delta] = [
'#markup' => $item->value,
];
}
return $element;
}
}

View File

@ -0,0 +1,7 @@
field.formatter.settings.{{ plugin_id }}:
type: mapping
label: {{ plugin_label }} formatter settings
mapping:
foo:
type: string
label: Foo

View File

@ -0,0 +1,27 @@
{% if configurable_storage %}
field.storage_settings.{{ plugin_id }}:
type: mapping
label: {{ plugin_label }} storage settings
mapping:
foo:
type: string
label: Foo
{% endif %}
{% if configurable_instance %}
field.field_settings.{{ plugin_id }}:
type: mapping
label: {{ plugin_label }} field settings
mapping:
bar:
type: string
label: Bar
{% endif %}
field.value.{{ plugin_id }}:
type: mapping
label: Default value
mapping:
value:
type: label
label: Value

View File

@ -0,0 +1,151 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
{% apply sort_namespaces %}
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
{% if configurable_storage or configurable_instance %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
{% endapply %}
/**
* Defines the '{{ plugin_id }}' field type.
*/
#[FieldType(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
description: new TranslatableMarkup('Some description'),
default_widget: 'string_textfield',
default_formatter: 'string',
)]
final class {{ class }} extends FieldItemBase {
{% if configurable_storage %}
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings(): array {
$settings = ['foo' => ''];
return $settings + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data): array {
$element['foo'] = [
'#type' => 'textfield',
'#title' => $this->t('Foo'),
'#default_value' => $this->getSetting('foo'),
'#disabled' => $has_data,
];
return $element;
}
{% endif %}
{% if configurable_instance %}
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings(): array {
$settings = ['bar' => ''];
return $settings + parent::defaultFieldSettings();
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state): array {
$element['bar'] = [
'#type' => 'textfield',
'#title' => $this->t('Bar'),
'#default_value' => $this->getSetting('bar'),
];
return $element;
}
{% endif %}
/**
* {@inheritdoc}
*/
public function isEmpty(): bool {
return match ($this->get('value')->getValue()) {
NULL, '' => TRUE,
default => FALSE,
};
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition): array {
// @DCG
// See /core/lib/Drupal/Core/TypedData/Plugin/DataType directory for
// available data types.
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Text value'))
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public function getConstraints(): array {
$constraints = parent::getConstraints();
$constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
// @DCG Suppose our value must not be longer than 10 characters.
$options['value']['Length']['max'] = 10;
// @DCG
// See /core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint
// directory for available constraints.
$constraints[] = $constraint_manager->create('ComplexData', $options);
return $constraints;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition): array {
$columns = [
'value' => [
'type' => 'varchar',
'not null' => FALSE,
'description' => 'Column description.',
'length' => 255,
],
];
$schema = [
'columns' => $columns,
// @todo Add indexes here if necessary.
];
return $schema;
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition): array {
$random = new Random();
$values['value'] = $random->word(mt_rand(1, 50));
return $values;
}
}

View File

@ -0,0 +1,7 @@
field.widget.settings.{{ plugin_id }}:
type: mapping
label: {{ plugin_label }} widget settings
mapping:
foo:
type: string
label: Foo

View File

@ -0,0 +1,99 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
{% apply sort_namespaces %}
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Defines the '{{ plugin_id }}' field widget.
*/
#[FieldWidget(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
field_types: ['string'],
)]
final class {{ class }} extends WidgetBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
$setting = ['foo' => 'bar'];
return $setting + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$element['foo'] = [
'#type' => 'textfield',
'#title' => $this->t('Foo'),
'#default_value' => $this->getSetting('foo'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary(): array {
return [
$this->t('Foo: @foo', ['@foo' => $this->getSetting('foo')]),
];
}
{% endif %}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => $items[$delta]->value ?? NULL,
];
return $element;
}
}

View File

@ -0,0 +1,97 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\migrate\destination;
{% apply sort_namespaces %}
use Drupal\migrate\Attribute\MigrateDestination;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* The '{{ plugin_id }}' destination plugin.
*/
#[MigrateDestination('{{ plugin_id }}')]
final class {{ class }} extends DestinationBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MigrationInterface $migration,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
MigrationInterface $migration = NULL,
): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
{{ di.container(services) }}
);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function getIds(): array {
$ids['id']['type'] = [
'type' => 'integer',
'unsigned' => TRUE,
'size' => 'big',
];
return $ids;
}
/**
* {@inheritdoc}
*/
public function fields(?MigrationInterface $migration = NULL): array {
return [
'id' => $this->t('The row ID.'),
// @todo Describe row fields here.
];
}
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []): array|bool {
// @todo Import the row here.
return [$row->getDestinationProperty('id')];
}
/**
* {@inheritdoc}
*/
public function rollback(array $destination_identifier): void {
// @todo Rollback the row here.
}
}

View File

@ -0,0 +1,69 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\migrate\process;
{% apply sort_namespaces %}
use Drupal\migrate\Attribute\MigrateProcess;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Provides {{ plugin_id|article }} plugin.
*
* Usage:
*
* @code
* process:
* bar:
* plugin: {{ plugin_id }}
* source: foo
* @endcode
*/
#[MigrateProcess('{{ plugin_id }}')]
final class {{ class }} extends ProcessPluginBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property): mixed {
// @todo Transform the value here.
return $value;
}
}

View File

@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\migrate\source;
{% if source_type == 'sql' %}
use Drupal\Core\Database\Query\SelectInterface;
{% endif %}
use Drupal\migrate\Plugin\migrate\source\{{ base_class }};
use Drupal\migrate\Row;
/**
* The '{{ plugin_id }}' source plugin.
*
* @MigrateSource(
* id = "{{ plugin_id }}",
* source_module = "{{ machine_name }}",
* )
*/
final class {{ class }} extends {{ base_class }} {
{% if source_type == 'sql' %}
/**
* {@inheritdoc}
*/
public function query(): SelectInterface {
return $this->select('example', 'e')
->fields('e', ['id', 'name', 'status']);
}
{% else %}
/**
* {@inheritdoc}
*/
public function __toString(): string {
// @DCG You may return something meaningful here.
return '';
}
/**
* {@inheritdoc}
*/
protected function initializeIterator(): \ArrayIterator {
// @DCG
// In this example we return a hardcoded set of records.
//
// For large sets of data consider using generators like follows:
// @code
// foreach ($foo->nextRecord() as $record) {
// yield $record;
// }
// @endcode
$records = [
[
'id' => 1,
'name' => 'Alpha',
'status' => TRUE,
],
[
'id' => 2,
'name' => 'Beta',
'status' => FALSE,
],
[
'id' => 3,
'name' => 'Gamma',
'status' => TRUE,
],
];
return new \ArrayIterator($records);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function fields(): array {
return [
'id' => $this->t('The record ID.'),
'name' => $this->t('The record name.'),
'status' => $this->t('The record status'),
];
}
/**
* {@inheritdoc}
*/
public function getIds(): array {
$ids['id'] = [
'type' => 'integer',
'unsigned' => TRUE,
'size' => 'big',
];
return $ids;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row): bool {
// @DCG
// Modify the row here if needed.
// Example:
// @code
// $name = $row->getSourceProperty('name');
// $row->setSourceProperty('name', Html::escape('$name'));
// @endcode
return parent::prepareRow($row);
}
}

View File

@ -0,0 +1,7 @@
views.argument_default.{{ plugin_id }}:
type: mapping
label: '{{ plugin_label }}'
mapping:
example:
type: string
label: 'Example'

View File

@ -0,0 +1,113 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\views\argument_default;
{% apply sort_namespaces %}
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsArgumentDefault;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
{% if services %}
{{ di.use(services) }}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* @todo Add plugin description here.
*/
#[ViewsArgumentDefault(
id: '{{ plugin_id }}',
title: new TranslatableMarkup('{{ plugin_label }}'),
)]
final class {{ class }} extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['example'] = ['default' => ''];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state): void {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->options['example'],
];
}
{% endif %}
/**
* {@inheritdoc}
*
* @todo Make sure the return type-hint matches the argument type.
*/
public function getArgument(): int {
// @DCG
// Here is the place where you should create a default argument for the
// contextual filter. The source of this argument depends on your needs.
// For example, the argument can be extracted from the URL or fetched from
// some fields of the currently viewed entity.
$argument = 123;
return $argument;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge(): int {
return Cache::PERMANENT;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts(): array {
// @todo Use 'url.path' or 'url.query_args:%key' contexts if the argument
// comes from URL.
return [];
}
}

View File

@ -0,0 +1,108 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\views\field;
{% apply sort_namespaces %}
use Drupal\Component\Render\MarkupInterface;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
{% if services %}
{{ di.use(services) }}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Provides {{ plugin_label }} field handler.
*
* @DCG
* The plugin needs to be assigned to a specific table column through
* hook_views_data() or hook_views_data_alter().
* Put the following code to {{ machine_name }}.views.inc file.
* @code
* function foo_views_data_alter(array &$data): void {
* $data['node']['foo_example']['field'] = [
* 'title' => t('Example'),
* 'help' => t('Custom example field.'),
* 'id' => 'foo_example',
* ];
* }
* @endcode
*/
#[ViewsField('{{ plugin_id }}')]
final class {{ class }} extends FieldPluginBase {
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['example'] = ['default' => ''];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state): void {
parent::buildOptionsForm($form, $form_state);
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->options['example'],
];
}
{% endif %}
/**
* {@inheritdoc}
*/
public function query(): void {
// For non-existent columns (i.e. computed fields) this method must be
// empty.
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values): string|MarkupInterface {
$value = parent::render($values);
// @todo Modify or replace the rendered value here.
return $value;
}
}

View File

@ -0,0 +1,7 @@
views.field.{{ plugin_id }}:
type: views.field.field
label: '{{ plugin_label }}'
mapping:
example:
type: string
label: 'Example'

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* @file
* Primary module hooks for {{ name }} module.
*/
use Drupal\Core\Template\Attribute;
/**
* Prepares variables for views-style-{{ plugin_id|u2h }}.html.twig template.
*/
function template_preprocess_views_style_{{ plugin_id }}(array &$variables): void {
$view = $variables['view'];
$options = $view->style_plugin->options;
{% if configurable %}
// Fetch wrapper classes from handler options.
if ($options['wrapper_class']) {
$variables['attributes']['class'] = explode(' ', $options['wrapper_class']);
}
{% endif %}
$variables['default_row_class'] = $options['default_row_class'];
foreach ($variables['rows'] as $id => $row) {
$variables['rows'][$id] = [
'content' => $row,
'attributes' => new Attribute(),
];
if ($row_class = $view->style_plugin->getRowClass($id)) {
$variables['rows'][$id]['attributes']->addClass($row_class);
}
}
}

View File

@ -0,0 +1,7 @@
views.style.{{ plugin_id }}:
type: views_style
label: '{{ plugin_label }}'
mapping:
wrapper_class:
type: string
label: 'Wrapper class'

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\views\style;
{% apply sort_namespaces %}
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsStyle;
{% endapply %}
/**
* {{ plugin_label }} style plugin.
*/
#[ViewsStyle(
id: '{{ plugin_id }}',
title: new TranslatableMarkup('{{ plugin_label }}'),
help: new TranslatableMarkup('@todo Add help text here.'),
theme: 'views_style_{{ plugin_id }}',
display_types: ['normal'],
)]
final class {{ class }} extends StylePluginBase {
/**
* {@inheritdoc}
*/
protected $usesRowPlugin = TRUE;
/**
* {@inheritdoc}
*/
protected $usesRowClass = TRUE;
{% if configurable %}
/**
* {@inheritdoc}
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['wrapper_class'] = ['default' => 'item-list'];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state): void {
parent::buildOptionsForm($form, $form_state);
$form['wrapper_class'] = [
'#type' => 'textfield',
'#title' => $this->t('Wrapper class'),
'#description' => $this->t('The class to provide on the wrapper, outside rows.'),
'#default_value' => $this->options['wrapper_class'],
];
}
{% endif %}
}

View File

@ -0,0 +1,23 @@
{{ '{#' }}
/**
* @file
* Default theme implementation for a view template to display a list of rows.
*
* Available variables:
* - attributes: HTML attributes for the container.
* - rows: A list of rows.
* - attributes: The row's HTML attributes.
* - content: The row's contents.
* - title: The title of this group of rows. May be empty.
*
* @see template_preprocess_views_style_{{ plugin_id }}()
*/
{{ '#}' }}{% verbatim %}
<div{{ attributes }}>
{% set row_classes = [default_row_class ? 'views-row'] %}
{% for row in rows %}
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
{% endfor %}
</div>{% endverbatim %}

View File

@ -0,0 +1,124 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Action;
{% apply sort_namespaces %}
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
{% else %}
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
{% endif %}
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Provides {{ plugin_label|article }} action.
*
* @DCG
* For updating entity fields consider extending FieldUpdateActionBase.
* @see \Drupal\Core\Field\FieldUpdateActionBase
*
* @DCG
* In order to set up the action through admin interface the plugin has to be
* configurable.
* @see https://www.drupal.org/project/drupal/issues/2815301
* @see https://www.drupal.org/project/drupal/issues/2815297
*
* @DCG
* The whole action API is subject of change.
* @see https://www.drupal.org/project/drupal/issues/2011038
*/
#[Action(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
category: new TranslatableMarkup('{{ category }}'),
type: '{{ entity_type }}',
)]
final class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'ActionBase' }} {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return ['example' => ''];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->configuration['example'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['example'] = $form_state->getValue('example');
}
{% endif %}
/**
* {@inheritdoc}
*/
public function access($entity, ?AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$access = $entity->access('update', $account, TRUE)
->andIf($entity->get('field_example')->access('edit', $account, TRUE));
return $return_as_object ? $access : $access->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute(?ContentEntityInterface $entity = NULL): void {
$entity->set('field_example', 'New value')->save();
}
}

View File

@ -0,0 +1,7 @@
action.configuration.{{ plugin_id }}:
type: mapping
label: 'Configuration for "{{ plugin_label }}" action'
mapping:
example:
type: string
label: Example

View File

@ -0,0 +1,112 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Block;
{% apply sort_namespaces %}
{% if access %}
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
{% endif %}
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Provides {{ plugin_label|article|lower }} block.
*/
#[Block(
id: '{{ plugin_id }}',
admin_label: new TranslatableMarkup('{{ plugin_label }}'),
category: new TranslatableMarkup('{{ category }}'),
)]
final class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'example' => $this->t('Hello world!'),
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state): array {
$form['example'] = [
'#type' => 'textarea',
'#title' => $this->t('Example'),
'#default_value' => $this->configuration['example'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state): void {
$this->configuration['example'] = $form_state->getValue('example');
}
{% endif %}
/**
* {@inheritdoc}
*/
public function build(): array {
$build['content'] = [
'#markup' => $this->t('It works!'),
];
return $build;
}
{% if access %}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account): AccessResult {
// @todo Evaluate the access condition here.
return AccessResult::allowedIf(TRUE);
}
{% endif %}
}

View File

@ -0,0 +1,7 @@
block.settings.{{ plugin_id }}:
type: block_settings
label: '{{ plugin_label }} block'
mapping:
example:
type: string
label: Example

View File

@ -0,0 +1,99 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Condition;
{% apply sort_namespaces %}
use Drupal\Core\Condition\Attribute\Condition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Provides a '{{ plugin_label }}' condition.
*/
#[Condition(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
)]
final class {{ class }} extends ConditionPluginBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return ['example' => ''] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->configuration['example'],
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['example'] = $form_state->getValue('example');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function summary(): string {
return (string) $this->t(
'Example: @example', ['@example' => $this->configuration['example']],
);
}
/**
* {@inheritdoc}
*/
public function evaluate(): bool {
// @todo Evaluate the condition here.
return TRUE;
}
}

View File

@ -0,0 +1,7 @@
condition.plugin.{{ plugin_id }}:
type: condition.plugin
label: '{{ plugin_label }} condition'
mapping:
age:
type: integer
label: Age

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
/**
* Provides {{ plugin_label|article }} constraint.
{% if input_type == 'entity' %}
*
* @see https://www.drupal.org/node/2015723.
{% elseif input_type == 'item_list' %}
*
* @DCG
* To apply this constraint on third party entity types implement either
* hook_entity_base_field_info_alter() or hook_entity_bundle_field_info_alter().
*
* @see https://www.drupal.org/node/2015723
{% elseif input_type == 'item' %}
*
* @DCG
* To apply this constraint on third party field types. Implement
* hook_field_info_alter() as follows.
* @code
* function {{ machine_name }}_field_info_alter(array &$info): void {
* $info['FIELD_TYPE']['constraints']['{{ plugin_id }}'] = [];
* }
* @endcode
*
* @see https://www.drupal.org/node/2015723
{% endif %}
*/
#[Constraint(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}', options: ['context' => 'Validation'])
)]
final class {{ class }} extends SymfonyConstraint {
public string $message = '@todo Specify error message here.';
}

View File

@ -0,0 +1,106 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
{% apply sort_namespaces %}
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
{% if input_type == 'item' %}
use Drupal\Core\Field\FieldItemInterface;
{% elseif input_type == 'item_list' %}
use Drupal\Core\Field\FieldItemListInterface;
{% elseif input_type == 'entity' %}
use Drupal\Core\Entity\EntityInterface;
{% endif %}
{% if services %}
{{ di.use(services) }}
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Validates the {{ plugin_label }} constraint.
*/
final class {{ class }}Validator extends ConstraintValidator {% if services %}implements ContainerInjectionInterface {% endif %}{
{% if services %}
/**
* Constructs the object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self(
{{ di.container(services) }}
);
}
{% endif %}
/**
* {@inheritdoc}
*/
{% if input_type == 'raw_value' %}
public function validate(mixed $value, Constraint $constraint): void {
// @todo Validate the value here.
if ($value === 'wrong') {
$this->context->addViolation($constraint->message);
}
}
{% elseif input_type == 'item' %}
public function validate(mixed $item, Constraint $constraint): void {
if (!$item instanceof FieldItemInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemInterface, %s was given.', get_debug_type($item))
);
}
// @todo Validate the item value here.
if ($item->value === 'wrong') {
$this->context->addViolation($constraint->message);
}
}
{% elseif input_type == 'item_list' %}
public function validate(mixed $items, Constraint $constraint): void {
if (!$items instanceof FieldItemListInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items))
);
}
foreach ($items as $delta => $item) {
// @todo Validate the item list here.
if ($item->value === 'wrong') {
$this->context->buildViolation($constraint->message)
->atPath($delta)
->addViolation();
}
}
}
{% elseif input_type == 'entity' %}
public function validate(mixed $entity, Constraint $constraint): void {
if (!$entity instanceof EntityInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Entity\EntityInterface, %s was given.', get_debug_type($entity))
);
}
// @todo Validate the entity here.
if ($entity->label() === 'wrong') {
// @DCG Use the following code to bind the violation to a specific field.
// @code
// $this->context->buildViolation($constraint->message)
// ->atPath('field_example')
// ->addViolation();
// @endcode
$this->context->addViolation($constraint->message);
}
}
{% endif %}
}

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\EntityReferenceSelection;
{% apply sort_namespaces %}
use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use {{ base_class_full }}{% if base_class == class %} as Base{{ base_class }}{% endif %};
{% endapply %}
/**
* @todo Add plugin description here.
*/
#[EntityReferenceSelection(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
group: '{{ plugin_id }}',
weight: 1,
entity_types: ['{{ entity_type }}'],
)]
final class {{ class }} extends {{ base_class == class ? 'Base' ~ base_class : base_class }} {
{% if configurable %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
$default_configuration = [
'foo' => 'bar',
];
return $default_configuration + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
$form['foo'] = [
'#type' => 'textfield',
'#title' => $this->t('Foo'),
'#default_value' => $this->configuration['foo'],
];
return $form;
}
{% endif %}
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS'): QueryInterface {
$query = parent::buildEntityQuery($match, $match_operator);
// @todo Modify the query here.
return $query;
}
}

View File

@ -0,0 +1,10 @@
entity_reference_selection.{{ plugin_id }}:
{# User selection plugin provides has some additional options. #}
type: entity_reference_selection.default{{ entity_type == 'user' ? ':user' }}
label: '{{ plugin_label }} handler settings'
{% if configurable %}
mapping:
foo:
type: string
label: Foo
{% endif %}

View File

@ -0,0 +1,96 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Filter;
{% apply sort_namespaces %}
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\filter\Attribute\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* @todo Add filter description here.
*/
#[Filter(
id: '{{ plugin_id }}',
title: new TranslatableMarkup('{{ plugin_label }}'),
type: FilterInterface::{{ filter_type }},
{% if configurable %}
settings: ['example' => 'foo'],
{% endif %}
)]
final class {{ class }} extends FilterBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->settings['example'],
'#description' => $this->t('Description of the setting.'),
];
return $form;
}
{% endif %}
/**
* {@inheritdoc}
*/
public function process($text, $langcode): FilterProcessResult {
// @todo Process text here.
{% if SUT_TEST %}
$text = \str_replace('foo', 'bar', $text);
{% endif %}
return new FilterProcessResult($text);
}
/**
* {@inheritdoc}
*/
public function tips($long = FALSE): string {
return (string) $this->t('@todo Provide filter tips here.');
}
}

View File

@ -0,0 +1,7 @@
filter_settings.{{ plugin_id }}:
type: filter
label: '{{ plugin_label }} filter'
mapping:
example:
type: string
label: Example

View File

@ -0,0 +1,71 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\Menu;
{% apply sort_namespaces %}
use Drupal\Core\Menu\MenuLinkDefault;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* @todo Provide description for this class.
*
* @DCG
* Typically a module-defined menu link relies on
* \Drupal\Core\Menu\MenuLinkDefault class that builds the link using plugin
* definitions located in YAML files (MODULE_NAME.links.menu.yml). The purpose
* of having custom menu link class is to make the link dynamic. Sometimes, the
* title and the URL of a link should vary based on some context, i.e. user
* being logged, current page URL, etc. Check out the parent classes for the
* methods you can override to make the link dynamic.
*
* @DCG It is important to supply the link with correct cache metadata.
* @see self::getCacheContexts()
* @see self::getCacheTags()
*
* @DCG
* You can apply the class to a link as follows.
* @code
* foo.example:
* title: Example
* route_name: foo.example
* menu_name: main
* class: \Drupal\foo\Plugin\Menu\FooMenuLink
* @endcode
*/
final class {{ class }} extends MenuLinkDefault {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
}

View File

@ -0,0 +1,62 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\QueueWorker;
{% apply sort_namespaces %}
use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endapply %}
/**
* Defines '{{ plugin_id }}' queue worker.
*/
#[QueueWorker(
id: '{{ plugin_id }}',
title: new TranslatableMarkup('{{ plugin_label }}'),
cron: ['time' => 60],
)]
final class {{ class }} extends QueueWorkerBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
/**
* Constructs a new {{ class }} instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
{{ di.signature(services) }}
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function processItem($data): void {
// @todo Process data here.
}
}

View File

@ -0,0 +1,157 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\rest\resource;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest\Attribute\RestResource;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Route;
/**
* Represents {{ plugin_label }} records as resources.
*
* @DCG
* The plugin exposes key-value records as REST resources. In order to enable it
* import the resource configuration into active configuration storage. An
* example of such configuration can be located in the following file:
* core/modules/rest/config/optional/rest.resource.entity.node.yml.
* Alternatively, you can enable it through admin interface provider by REST UI
* module.
* @see https://www.drupal.org/project/restui
*
* @DCG
* Notice that this plugin does not provide any validation for the data.
* Consider creating custom normalizer to validate and normalize the incoming
* data. It can be enabled in the plugin definition as follows.
* @code
* serialization_class = "Drupal\foo\MyDataStructure",
* @endcode
*
* @DCG
* For entities, it is recommended to use REST resource plugin provided by
* Drupal core.
* @see \Drupal\rest\Plugin\rest\resource\EntityResource
*/
#[RestResource(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
uri_paths: [
'canonical' => '/api/{{ plugin_id|u2h }}/{id}',
'create' => '/api/{{ plugin_id|u2h }}',
],
)]
final class {{ class }} extends ResourceBase {
/**
* The key-value storage.
*/
private readonly KeyValueStoreInterface $storage;
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
KeyValueFactoryInterface $keyValueFactory,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->storage = $keyValueFactory->get('{{ plugin_id }}');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('rest'),
$container->get('keyvalue')
);
}
/**
* Responds to POST requests and saves the new record.
*/
public function post(array $data): ModifiedResourceResponse {
$data['id'] = $this->getNextId();
$this->storage->set($data['id'], $data);
$this->logger->notice('Created new {{ plugin_label|lower }} record @id.', ['@id' => $data['id']]);
// Return the newly created record in the response body.
return new ModifiedResourceResponse($data, 201);
}
/**
* Responds to GET requests.
*/
public function get($id): ResourceResponse {
if (!$this->storage->has($id)) {
throw new NotFoundHttpException();
}
$resource = $this->storage->get($id);
return new ResourceResponse($resource);
}
/**
* Responds to PATCH requests.
*/
public function patch($id, array $data): ModifiedResourceResponse {
if (!$this->storage->has($id)) {
throw new NotFoundHttpException();
}
$stored_data = $this->storage->get($id);
$data += $stored_data;
$this->storage->set($id, $data);
$this->logger->notice('The {{ plugin_label|lower }} record @id has been updated.', ['@id' => $id]);
return new ModifiedResourceResponse($data, 200);
}
/**
* Responds to DELETE requests.
*/
public function delete($id): ModifiedResourceResponse {
if (!$this->storage->has($id)) {
throw new NotFoundHttpException();
}
$this->storage->delete($id);
$this->logger->notice('The {{ plugin_label|lower }} record @id has been deleted.', ['@id' => $id]);
// Deleted responses have an empty body.
return new ModifiedResourceResponse(NULL, 204);
}
/**
* {@inheritdoc}
*/
protected function getBaseRoute($canonical_path, $method): Route {
$route = parent::getBaseRoute($canonical_path, $method);
// Set ID validation pattern.
if ($method !== 'POST') {
$route->setRequirement('id', '\d+');
}
return $route;
}
/**
* Returns next available ID.
*/
private function getNextId(): int {
$ids = \array_keys($this->storage->getAll());
return count($ids) > 0 ? max($ids) + 1 : 1;
}
}

View File

@ -0,0 +1,59 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Access;
{% apply sort_namespaces %}
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Symfony\Component\Routing\Route;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* Checks if passed parameter matches the route configuration.
*
* Usage example:
* @code
* foo.example:
* path: '/example/{parameter}'
* defaults:
* _title: 'Example'
* _controller: '\Drupal\{{ machine_name }}\Controller\{{ machine_name|camelize }}Controller'
* requirements:
* {{ requirement }}: 'some value'
* @endcode
*/
final class {{ class }} implements AccessInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* Access callback.
*
* @DCG
* Drupal does some magic when resolving arguments for this callback. Make
* sure the parameter name matches the name of the placeholder defined in the
* route, and it is of the same type.
* The following additional parameters are resolved automatically.
* - \Drupal\Core\Routing\RouteMatchInterface
* - \Drupal\Core\Session\AccountInterface
* - \Symfony\Component\HttpFoundation\Request
* - \Symfony\Component\Routing\Route
*/
public function access(Route $route, mixed $parameter): AccessResult {
return AccessResult::allowedIf($parameter === $route->getRequirement('{{ requirement }}'));
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
access_check.{{ machine_name }}.{{ requirement|trim('_') }}:
class: Drupal\{{ machine_name }}\Access\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: access_check, applies_to: {{ requirement }} }

View File

@ -0,0 +1,54 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% apply sort_namespaces %}
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add description for this breadcrumb builder.
*/
final class {{ class }} implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match): bool {
return $route_match->getRouteName() === 'example';
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match): Breadcrumb {
$breadcrumb = new Breadcrumb();
$links[] = Link::createFromRoute($this->t('Home'), '<front>');
$links[] = Link::createFromRoute($this->t('Example'), '<none>');
return $breadcrumb->setLinks($links);
}
}

View File

@ -0,0 +1,11 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.breadcrumb:
class: Drupal\{{ machine_name }}\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
# In order to override breadcrumbs built with PathBasedBreadcrumbBuilder
# set the priority higher than zero.
- { name: breadcrumb_builder, priority: 1000 }

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Cache\Context;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\{{ interface }};
{% if base_class %}
use Drupal\Core\Cache\Context\{{ base_class }};
{% endif %}
/**
* @todo Add a description for the cache context.
*
* Cache context ID: '{{ context_id }}'.
*
* @DCG
* Check out the core/lib/Drupal/Core/Cache/Context directory for examples of
* cache contexts provided by Drupal core.
*/
final class {{ class }} {% if base_class %}extends {{ base_class }} {% endif %}implements {{ interface }} {
/**
* {@inheritdoc}
*/
public static function getLabel(): string {
return (string) t('{{ context_label }}');
}
/**
* {@inheritdoc}
*/
public function getContext({% if calculated %}$parameter = NULL{% endif %}): string {
// @todo Calculate the cache context here.
$context = 'some_string_value';
return $context;
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata({% if calculated %}$parameter = NULL{% endif %}): CacheableMetadata {
return new CacheableMetadata();
}
}

View File

@ -0,0 +1,10 @@
services:
cache_context.{{ context_id }}:
class: Drupal\{{ machine_name }}\Cache\Context\{{ class }}
{% if base_class == 'RequestStackCacheContextBase' %}
arguments: ['@request_stack']
{% elseif base_class == 'UserCacheContextBase' %}
arguments: ['@current_user']
{% endif %}
tags:
- { name: cache.context}

View File

@ -0,0 +1,32 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% if services %}
{{ di.use(services) }}
{% endif %}
/**
* @todo Add class description.
*/
final class {{ class }}{{ interface ? ' implements ' ~ interface }} {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {{ interface ? '{@inheritdoc}' : '@todo Add method description.'}}
*/
public function doSomething(): void {
// @todo Place your code here.
}
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
/**
* @todo Add interface description.
*/
interface {{ interface }} {
/**
* @todo Add method description.
*/
public function doSomething(): void;
}

View File

@ -0,0 +1,7 @@
{% import '@lib/di.twig' as di %}
services:
{{ service_name }}:
class: Drupal\{{ machine_name }}\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}

View File

@ -0,0 +1,62 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\EventSubscriber;
{% apply sort_namespaces %}
{% if services %}
{{ di.use(services) }}
{% endif %}
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
{% endapply %}
/**
* @todo Add description for this subscriber.
*/
final class {{ class }} implements EventSubscriberInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* Kernel request event handler.
*/
public function onKernelRequest(RequestEvent $event): void {
// @todo Place your code here.
{% if SUT_TEST %}
$this->messenger->addStatus(__FUNCTION__);
{% endif %}
}
/**
* Kernel response event handler.
*/
public function onKernelResponse(ResponseEvent $event): void {
// @todo Place your code here.
{% if SUT_TEST %}
$this->messenger->addStatus(__FUNCTION__);
{% endif %}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::REQUEST => ['onKernelRequest'],
KernelEvents::RESPONSE => ['onKernelResponse'],
];
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.event_subscriber:
class: Drupal\{{ machine_name }}\EventSubscriber\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: event_subscriber }

View File

@ -0,0 +1,49 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Logger;
{% apply sort_namespaces %}
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add a description for the logger.
*/
final class {{ class }} implements LoggerInterface {
use RfcLoggerTrait;
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function log($level, string|\Stringable $message, array $context = []): void {
$message = (string) $message;
// Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
// style, so they can be translated too.
$placeholders = $this->parser->parseMessagePlaceholders($message, $context);
// @see \Drupal\Core\Logger\LoggerChannel::log() for all available contexts.
$rendered_message = strtr($message, $placeholders);
// @todo Log the rendered message here.
{% if SUT_TEST %}
\file_put_contents('temporary://logger_test.log', $level . ' -> ' . $rendered_message);
{% endif %}
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
logger.{{ service_id }}:
class: Drupal\{{ machine_name }}\Logger\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: logger }

View File

@ -0,0 +1,44 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% apply sort_namespaces %}
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add a description for the middleware.
*/
final class {{ class }} implements HttpKernelInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
// @todo Modify the request here.
$response = $this->httpKernel->handle($request, $type, $catch);
// @todo Modify the response here.
{% if SUT_TEST %}
$response->headers->set('x-middleware-handle-test', NULL);
{% endif %}
return $response;
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.middleware:
class: Drupal\{{ machine_name }}\{{ class }}
{% if service_arguments %}
arguments: [{{ di.arguments(service_arguments) }}]
{% endif %}
tags:
- { name: http_middleware, priority: 10 }

View File

@ -0,0 +1,71 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% apply sort_namespaces %}
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\Routing\Route;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add description for the converter.
*
* @DCG
* To use this converter specify parameter type in a relevant route as follows:
* @code
* {{ machine_name }}.{{ parameter_type }}_parameter_converter:
* path: example/{record}
* defaults:
* _controller: '\Drupal\{{ machine_name }}\Controller\{{ controller_class }}::build'
* requirements:
* _access: 'TRUE'
* options:
* parameters:
* record:
* type: {{ parameter_type }}
* @endcode
*
* Note that parameter converter for entities already exists in Drupal core.
* @see \Drupal\Core\ParamConverter\EntityConverter
*/
final class {{ class }} implements ParamConverterInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function convert($value, $definition, $name, array $defaults): ?string {
// @DCG
// If the converter returns something different rather than strings make
// sure to define an appropriate return type for this method.
return match ($value) {
'1' => 'alpha',
'2' => 'beta',
'3' => 'gamma',
// NULL will trigger 404 HTTP error.
default => NULL,
};
}
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route): bool {
return isset($definition['type']) && $definition['type'] === 'example';
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.{{ parameter_type }}_param_converter:
class: Drupal\{{ machine_name }}\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: paramconverter }

View File

@ -0,0 +1,49 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\PathProcessor;
{% apply sort_namespaces %}
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* Path processor to replace 'node' with 'content' in URLs.
*
* @DCG In case you need to implement only one processor (inbound or outbound)
* remove the corresponding interface, method and service tag.
*/
final class {{ class }} implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function processInbound($path, Request $request): string {
return preg_replace('#^/content/#i', '/node/', $path);
}
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL): string {
return preg_replace('#^/node/#i', '/content/', $path);
}
}

View File

@ -0,0 +1,10 @@
{% import '@lib/di.twig' as di %}
services:
path_processor.{{ machine_name }}:
class: Drupal\{{ machine_name }}\PathProcessor\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: path_processor_inbound }
- { name: path_processor_outbound }

View File

@ -0,0 +1,41 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\PageCache;
{% apply sort_namespaces %}
use Drupal\Core\PageCache\RequestPolicyInterface;
use Symfony\Component\HttpFoundation\Request;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add policy description here.
*/
final class {{ class }} implements RequestPolicyInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function check(Request $request): ?string {
// @DCG
// Return self::ALLOW or self::DENY to indicate whether delivery of a cached
// page should be attempted for this request. Return NULL if the policy does
// not apply to the given request.
return NULL;
}
}

View File

@ -0,0 +1,9 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.page_cache_request_policy.{{ class|c2m }}:
class: Drupal\{{ machine_name }}\PageCache\{{ class }}
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: page_cache_request_policy }

View File

@ -0,0 +1,41 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\PageCache;
{% apply sort_namespaces %}
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endapply %}
/**
* @todo Add policy description here.
*/
final class {{ class }} implements ResponsePolicyInterface {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
public function check(Response $response, Request $request): ?string {
// @DCG
// Return self::DENY to indicate that the response should not be stored in
// the cache. Return NULL if the policy does not apply to the given request.
return NULL;
}
}

View File

@ -0,0 +1,11 @@
{% import '@lib/di.twig' as di %}
services:
{{ machine_name }}.page_cache_response_policy.{{ class|c2m }}:
class: Drupal\{{ machine_name }}\PageCache\{{ class }}
public: false
{% if services %}
arguments: [{{ di.arguments(services) }}]
{% endif %}
tags:
- { name: page_cache_response_policy }
- { name: dynamic_page_cache_response_policy }

View File

@ -0,0 +1,37 @@
{% import '@lib/di.twig' as di %}
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\EventSubscriber;
{% apply sort_namespaces %}
{% if services %}
{{ di.use(services) }}
{% endif %}
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
{% endapply %}
/**
* Route subscriber.
*/
final class {{ class }} extends RouteSubscriberBase {
{% if services %}
/**
* Constructs {{ class|article }} object.
*/
public function __construct(
{{ di.signature(services) }}
) {}
{% endif %}
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection): void {
// @see https://www.drupal.org/node/2187643
}
}

Some files were not shown because too many files have changed in this diff Show More