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,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 %}