default services conflit ?

This commit is contained in:
armansansd
2022-04-27 11:30:43 +02:00
parent 28190a5749
commit 8bb1064a3b
8132 changed files with 900138 additions and 426 deletions

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,13 @@
name: {{ name }}
type: module
description: 'Provides {{ entity_type_label|article|lower }} configuration entity.'
package: {{ package }}
core: 8.x
core_version_requirement: ^8 || ^9
{% if dependencies %}
dependencies:
{% for dependency in dependencies %}
- {{ dependency }}
{% endfor %}
{% endif %}
configure: entity.{{ entity_type_id }}.collection

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|plural }}
parent: system.admin_structure
description: 'List of {{ entity_type_label|lower|plural }} 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,81 @@
<?php
namespace Drupal\{{ machine_name }}\Entity;
{% sort %}
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
{% endsort %}
/**
* Defines the {{ entity_type_label|lower }} entity type.
*
* @ConfigEntityType(
* id = "{{ entity_type_id }}",
* label = @Translation("{{ entity_type_label }}"),
* label_collection = @Translation("{{ entity_type_label|plural }}"),
* label_singular = @Translation("{{ entity_type_label|lower }}"),
* label_plural = @Translation("{{ entity_type_label|plural|lower }}"),
* label_count = @PluralTranslation(
* singular = "@count {{ entity_type_label|lower }}",
* plural = "@count {{ entity_type_label|plural|lower }}",
* ),
* handlers = {
* "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
* }
* },
* config_prefix = "{{ entity_type_id }}",
* admin_permission = "administer {{ entity_type_id }}",
* 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"
* },
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* config_export = {
* "id",
* "label",
* "description"
* }
* )
*/
class {{ class_prefix }} extends ConfigEntityBase implements {{ class_prefix }}Interface {
/**
* The {{ entity_type_label|lower }} ID.
*
* @var string
*/
protected $id;
/**
* The {{ entity_type_label|lower }} label.
*
* @var string
*/
protected $label;
/**
* The {{ entity_type_label|lower }} status.
*
* @var bool
*/
protected $status;
/**
* The {{ entity_type_id|lower }} description.
*
* @var string
*/
protected $description;
}

View File

@@ -0,0 +1,12 @@
<?php
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,34 @@
<?php
namespace Drupal\{{ machine_name }};
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of {{ entity_type_label|lower|plural }}.
*/
class {{ class_prefix }}ListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$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) {
/** @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,70 @@
<?php
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* {{ entity_type_label }} form.
*
* @property \Drupal\{{ machine_name }}\{{ class_prefix }}Interface $entity
*/
class {{ class_prefix }}Form extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity->label(),
'#description' => $this->t('Label for the {{ entity_type_label|lower }}.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity->id(),
'#machine_name' => [
'exists' => '\Drupal\{{ machine_name }}\Entity\{{ class_prefix }}::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'),
'#description' => $this->t('Description of the {{ entity_type_label|lower }}.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$result = parent::save($form, $form_state);
$message_args = ['%label' => $this->entity->label()];
$message = $result == SAVED_NEW
? $this->t('Created new {{ entity_type_label|lower }} %label.', $message_args)
: $this->t('Updated {{ entity_type_label|lower }} %label.', $message_args);
$this->messenger()->addStatus($message);
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
return $result;
}
}

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,15 @@
name: {{ name }}
type: module
description: 'Provides {{ entity_type_label|article|lower }} entity.'
package: {{ package }}
core: 8.x
core_version_requirement: ^8 || ^9
{% if dependencies %}
dependencies:
{% for dependency in dependencies %}
- {{ dependency }}
{% endfor %}
{% endif %}
{% if configure %}
configure: {{ configure }}
{% endif %}

View File

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

View File

@@ -0,0 +1,18 @@
{% 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
entity.{{ entity_type_id }}.collection:
title: {{ entity_type_label|plural }}
description: List of {{ entity_type_label|plural|lower }}
route_name: entity.{{ entity_type_id }}.collection
parent: system.admin_content
{% elseif bundle %}
entity.{{ entity_type_id }}_type.collection:
title: '{{ entity_type_label }} types'
parent: system.admin_structure
description: 'Manage and CRUD actions on {{ entity_type_label }} type.'
route_name: entity.{{ entity_type_id }}_type.collection
{% endif %}

View File

@@ -0,0 +1,34 @@
{% 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 %}
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
entity.{{ entity_type_id }}.collection:
title: {{ entity_type_label }}
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,36 @@
<?php
/**
* @file
* Provides {{ entity_type_label|article|lower }} entity type.
*/
use Drupal\Core\Render\Element;
/**
* Implements hook_theme().
*/
function {{ machine_name }}_theme() {
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) {
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}

View File

@@ -0,0 +1,22 @@
{% if bundle %}
administer {{ entity_type_label|lower }} types:
title: 'Administer {{ entity_type_label|lower }} types'
description: 'Maintain the types of {{ entity_type_label|lower }} entity.'
restrict access: true
{% elseif fieldable %}
administer {{ entity_type_label|lower }}:
title: 'Administer {{ entity_type_label|lower }} settings'
restrict access: true
{% endif %}
access {{ entity_type_label|lower }} overview:
title: 'Access {{ entity_type_label|lower }} overview page'
{% if access_controller %}
delete {{ entity_type_label|lower }}:
title: Delete {{ entity_type_label|lower }}
create {{ entity_type_label|lower }}:
title: Create {{ entity_type_label|lower }}
view {{ entity_type_label|lower }}:
title: View {{ entity_type_label|lower }}
edit {{ entity_type_label|lower }}:
title: Edit {{ entity_type_label|lower }}
{% 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_prefix }}SettingsForm'
_title: '{{ entity_type_label }}'
requirements:
_permission: 'administer {{ entity_type_label|lower }}'
{% endif %}

View File

@@ -0,0 +1,370 @@
<?php
namespace Drupal\{{ machine_name }}\Entity;
{% sort %}
{% if not revisionable %}
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
{% if author_base_field %}
use Drupal\Core\Entity\EntityStorageInterface;
{% endif %}
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
{% if revisionable %}
use Drupal\Core\Entity\RevisionableContentEntityBase;
{% endif %}
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
{% if author_base_field %}
use Drupal\user\UserInterface;
{% endif %}
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedTrait;
{% endif %}
{% endsort %}
/**
* Defines the {{ entity_type_label|lower }} entity class.
*
* @ContentEntityType(
* id = "{{ entity_type_id }}",
* label = @Translation("{{ entity_type_label }}"),
* label_collection = @Translation("{{ entity_type_label|plural }}"),
{% if bundle %}
* bundle_label = @Translation("{{ entity_type_label }} type"),
{% endif %}
* handlers = {
{% if template %}
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
{% else %}
* "view_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ViewBuilder",
{% endif %}
* "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
{% if access_controller %}
* "access" = "Drupal\{{ machine_name }}\{{ class_prefix }}AccessControlHandler",
{% endif %}
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* 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 revisionable %}
* show_revision_ui = TRUE,
{% endif %}
{% if translatable %}
* translatable = TRUE,
{% endif %}
{% if bundle %}
* admin_permission = "administer {{ entity_type_label|lower }} types",
{% elseif fieldable %}
* admin_permission = "administer {{ entity_type_label|lower }}",
{% else %}
* admin_permission = "access {{ entity_type_label|lower }} overview",
{% endif %}
* entity_keys = {
* "id" = "id",
{% if revisionable %}
* "revision" = "revision_id",
{% endif %}
{% if translatable %}
* "langcode" = "langcode",
{% endif %}
{% if bundle %}
* "bundle" = "bundle",
{% endif %}
* "label" = "{{ title_base_field ? 'title' : 'id' }}",
* "uuid" = "uuid"
* },
{% if revisionable %}
* revision_metadata_keys = {
{% if author_base_field %}
* "revision_user" = "revision_uid",
{% endif %}
{% if created_base_field %}
* "revision_created" = "revision_timestamp",
{% endif %}
* "revision_log_message" = "revision_log"
* },
{% endif %}
* links = {
{% 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_type_id }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
* "collection" = "/admin/content/{{ entity_type_id|u2h }}"
* },
{% if bundle %}
* bundle_entity_type = "{{ entity_type_id }}_type",
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form"
{% elseif fieldable %}
* field_ui_base_route = "entity.{{ entity_type_id }}.settings"
{% endif %}
* )
*/
class {{ class_prefix }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class_prefix }}Interface {
{% if changed_base_field %}
use EntityChangedTrait;
{% endif %}
{% if author_base_field %}
/**
* {@inheritdoc}
*
* When a new {{ entity_type_label|lower }} entity is created, set the uid entity reference to
* the current user as the creator of the entity.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += ['uid' => \Drupal::currentUser()->id()];
}
{% endif %}
{% if title_base_field %}
/**
* {@inheritdoc}
*/
public function getTitle() {
return $this->get('title')->value;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
{% endif %}
{% if status_base_field %}
/**
* {@inheritdoc}
*/
public function isEnabled() {
return (bool) $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setStatus($status) {
$this->set('status', $status);
return $this;
}
{% endif %}
{% if created_base_field %}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
{% endif %}
{% if author_base_field %}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
{% endif %}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
{% if title_base_field %}
$fields['title'] = BaseFieldDefinition::create('string')
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
{% if translatable %}
->setTranslatable(TRUE)
{% endif %}
->setLabel(t('Title'))
->setDescription(t('The title of the {{ entity_type_label|lower }} entity.'))
->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'))
->setDescription(t('A boolean indicating whether the {{ entity_type_label|lower }} is enabled.'))
->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'))
->setDescription(t('A description of the {{ entity_type_label|lower }}.'))
->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'))
->setDescription(t('The user ID of the {{ entity_type_label|lower }} author.'))
->setSetting('target_type', 'user')
->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;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Drupal\{{ machine_name }}\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
/**
* Defines the {{ entity_type_label }} type configuration entity.
*
* @ConfigEntityType(
* id = "{{ entity_type_id }}_type",
* label = @Translation("{{ entity_type_label }} type"),
* handlers = {
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}TypeForm",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}TypeForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* },
* "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}TypeListBuilder",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* admin_permission = "administer {{ entity_type_label|lower }} types",
* bundle_of = "{{ entity_type_id }}",
* config_prefix = "{{ entity_type_id }}_type",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* 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"
* },
* config_export = {
* "id",
* "label",
* "uuid",
* }
* )
*/
class {{ class_prefix }}Type extends ConfigEntityBundleBase {
/**
* The machine name of this {{ entity_type_label|lower }} type.
*
* @var string
*/
protected $id;
/**
* The human-readable name of the {{ entity_type_label|lower }} type.
*
* @var string
*/
protected $label;
}

View File

@@ -0,0 +1,44 @@
<?php
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.
*/
class {{ class_prefix }}AccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermission($account, 'view {{ entity_type_label|lower }}');
case 'update':
return AccessResult::allowedIfHasPermissions($account, ['edit {{ entity_type_label|lower }}', 'administer {{ entity_type_label|lower }}'], 'OR');
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete {{ entity_type_label|lower }}', 'administer {{ entity_type_label|lower }}'], 'OR');
default:
// No opinion.
return AccessResult::neutral();
}
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermissions($account, ['create {{ entity_type_label|lower }}', 'administer {{ entity_type_label|lower }}'], 'OR');
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Drupal\{{ machine_name }};
use Drupal\Core\Entity\ContentEntityInterface;
{% if author_base_field %}
use Drupal\user\EntityOwnerInterface;
{% endif %}
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedInterface;
{% endif %}
/**
* Provides an interface defining {{ entity_type_label|article|lower }} entity type.
*/
interface {{ class_prefix }}Interface extends ContentEntityInterface{% if author_base_field %}, EntityOwnerInterface{% endif %}{% if changed_base_field %}, EntityChangedInterface{% endif %} {
{% if title_base_field %}
/**
* Gets the {{ entity_type_label|lower }} title.
*
* @return string
* Title of the {{ entity_type_label|lower }}.
*/
public function getTitle();
/**
* Sets the {{ entity_type_label|lower }} title.
*
* @param string $title
* The {{ entity_type_label|lower }} title.
*
* @return \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
* The called {{ entity_type_label|lower }} entity.
*/
public function setTitle($title);
{% endif %}
{% if created_base_field %}
/**
* Gets the {{ entity_type_label|lower }} creation timestamp.
*
* @return int
* Creation timestamp of the {{ entity_type_label|lower }}.
*/
public function getCreatedTime();
/**
* Sets the {{ entity_type_label|lower }} creation timestamp.
*
* @param int $timestamp
* The {{ entity_type_label|lower }} creation timestamp.
*
* @return \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
* The called {{ entity_type_label|lower }} entity.
*/
public function setCreatedTime($timestamp);
{% endif %}
{% if status_base_field %}
/**
* Returns the {{ entity_type_label|lower }} status.
*
* @return bool
* TRUE if the {{ entity_type_label|lower }} is enabled, FALSE otherwise.
*/
public function isEnabled();
/**
* Sets the {{ entity_type_label|lower }} status.
*
* @param bool $status
* TRUE to enable this {{ entity_type_label|lower }}, FALSE to disable.
*
* @return \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
* The called {{ entity_type_label|lower }} entity.
*/
public function setStatus($status);
{% endif %}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace Drupal\{{ machine_name }};
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a list controller for the {{ entity_type_label|lower }} entity type.
*/
class {{ class_prefix }}ListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* Constructs a new {{ class_prefix }}ListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->redirectDestination = $redirect_destination;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('date.formatter'),
$container->get('redirect.destination')
);
}
/**
* {@inheritdoc}
*/
public function render() {
$build['table'] = parent::render();
$total = $this->getStorage()
->getQuery()
->count()
->execute();
$build['summary']['#markup'] = $this->t('Total {{ entity_type_label|lower|plural }}: @total', ['@total' => $total]);
return $build;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('ID');
{% if title_base_field %}
$header['title'] = $this->t('Title');
{% 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) {
/* @var $entity \Drupal\{{ machine_name }}\{{ class_prefix }}Interface */
$row['id'] = $entity->{{ title_base_field ? 'id' : 'toLink' }}();
{% if title_base_field %}
$row['title'] = $entity->toLink();
{% endif %}
{% if status_base_field %}
$row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled');
{% endif %}
{% if author_base_field %}
$row['uid']['data'] = [
'#theme' => 'username',
'#account' => $entity->getOwner(),
];
{% endif %}
{% if created_base_field %}
$row['created'] = $this->dateFormatter->format($entity->getCreatedTime());
{% endif %}
{% if changed_base_field %}
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime());
{% endif %}
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$destination = $this->redirectDestination->getAsArray();
foreach ($operations as $key => $operation) {
$operations[$key]['query'] = $destination;
}
return $operations;
}
}

View File

@@ -0,0 +1,51 @@
<?php
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_prefix }}Type
*/
class {{ class_prefix }}TypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['title'] = $this->t('Label');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['title'] = [
'data' => $entity->label(),
'class' => ['menu-label'],
];
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function render() {
$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,23 @@
<?php
namespace Drupal\{{ machine_name }};
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
/**
* Provides a view controller for {{ entity_type_label|article|lower }} entity type.
*/
class {{ class_prefix }}ViewBuilder extends EntityViewBuilder {
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
// The {{ entity_type_label|lower }} has no entity template itself.
unset($build['#theme']);
return $build;
}
}

View File

@@ -0,0 +1,37 @@
<?php
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.
*/
class {{ class_prefix }}Form extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->getEntity();
$result = $entity->save();
$link = $entity->toLink($this->t('View'))->toRenderable();
$message_arguments = ['%label' => $this->entity->label()];
$logger_arguments = $message_arguments + ['link' => render($link)];
if ($result == SAVED_NEW) {
$this->messenger()->addStatus($this->t('New {{ entity_type_label|lower }} %label has been created.', $message_arguments));
$this->logger('{{ machine_name }}')->notice('Created new {{ entity_type_label|lower }} %label', $logger_arguments);
}
else {
$this->messenger()->addStatus($this->t('The {{ entity_type_label|lower }} %label has been updated.', $message_arguments));
$this->logger('{{ machine_name }}')->notice('Updated new {{ entity_type_label|lower }} %label.', $logger_arguments);
}
$form_state->setRedirect('entity.{{ entity_type_id }}.canonical', ['{{ entity_type_id }}' => $entity->id()]);
}
}

View File

@@ -0,0 +1,48 @@
<?php
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.
*/
class {{ class_prefix }}SettingsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return '{{ entity_type_id }}_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['settings'] = [
'#markup' => $this->t('Settings form for {{ entity_type_label|article|lower }} entity type.'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->messenger()->addStatus($this->t('The configuration has been updated.'));
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Drupal\{{ machine_name }}\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Form handler for {{ entity_type_label|lower }} type forms.
*/
class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$entity_type = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
}
else {
$form['#title'] = $this->t(
'Edit %label {{ entity_type_label|lower }} type',
['%label' => $entity_type->label()]
);
}
$form['label'] = [
'#title' => $this->t('Label'),
'#type' => 'textfield',
'#default_value' => $entity_type->label(),
'#description' => $this->t('The human-readable name of this {{ entity_type_label|lower }} type.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity_type->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => ['Drupal\{{ machine_name }}\Entity\{{ class_prefix }}Type', '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) {
$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) {
$entity_type = $this->entity;
$entity_type->set('id', trim($entity_type->id()));
$entity_type->set('label', trim($entity_type->label()));
$status = $entity_type->save();
$t_args = ['%name' => $entity_type->label()];
if ($status == SAVED_UPDATED) {
$message = $this->t('The {{ entity_type_label|lower }} type %name has been updated.', $t_args);
}
elseif ($status == SAVED_NEW) {
$message = $this->t('The {{ entity_type_label|lower }} type %name has been added.', $t_args);
}
$this->messenger()->addStatus($message);
$form_state->setRedirectUrl($entity_type->toUrl('collection'));
}
}

View File

@@ -0,0 +1,21 @@
{{ '{#' }}
/**
* @file
* Default theme implementation to present {{ entity_type_label|article|lower }} entity.
*
* This template is used when viewing a registered {{ entity_type_label|lower }}'s page,
* e.g., {{ entity_base_path }})/123. 123 being the {{ entity_type_label|lower }}'s ID.
*
* Available variables:
* - content: A list of content items. Use 'content' to print all content, or
* print a subset such as 'content.title'.
* - attributes: HTML attributes for the container element.
*
* @see template_preprocess_{{ entity_type_id }}()
*/
{{ '#}' }}{% verbatim %}
<article{{ attributes }}>
{% if content %}
{{- content -}}
{% endif %}
</article>{% endverbatim %}