Upload
eugene-kulishov
View
232
Download
8
Embed Size (px)
Citation preview
Configuration entities in Drupal 8.
What we will speak about?
● Innovations in Entity Api.
● Configuration manager in Drupal 8
● Examples of use of configuration entities
● Creation of custom Config Entity on the example of Config Pages module
Types of information in Drupal 8
Content State Session Configuration
Config API Configuration Entity API
Status of modules
Site name
Content types
Image styles
Configuration entities in Drupal 8
● Views● Fields● Content types● Image styles● Display settings● Blocks● Role● Taxonomy vocabulary● Date format● Comment type● Text format● Date format
Configuration Manager interface
Configuration Manager interface
YAML format structure
node.type.page.yml
uuid: a0025874-17ec-4ad2-a300-0af31a8a462b
langcode: en
status: true
dependencies: { }
name: 'Basic page'
type: page
description: 'Use <em>basic pages</em> for your static content, such as an ''About us'' page.'
help: ''
new_revision: false
preview_mode: 1
display_submitted: false
Configuration installation
File: core.entity_view_mode.comment.simple_comment.yml
langcode: enstatus: falsedependencies: module: - commentid: comment.simple_commentlabel: 'My simple comment'targetEntityType: commentcache: true
Config API code examples
<?php
// Get data from config. $config = \Drupal::config('system.site');// Instance of \Drupal\Core\Config\ImmutableConfig
$front_page = $config->get('page.front');// /user/login
$front_page = \Drupal::config('system.site')->get('page.front');// /user/login
// Save data to config.$config = \Drupal::service('config.factory') ->getEditable('system.site');// Instance of \Drupal\Core\Config\Config
$config->set('page.front', 'new-front-page');$config->save();
Main stages of creation
● Definition of scheme and interface● Basic defenition of the class● We expand opportunities: add listing of
object, add CRUD forms● Removing of configuration entity
Schema of Configuration entity
File: config_pages.schema.ymlconfig_pages.type.*: type: config_entity label: 'Config page type settings' mapping: id: type: string label: 'ID' label: type: label label: 'Label'
Basic definition of the class
File: ConfigPagesType.phpnamespace Drupal\config_pages\Entity;use Drupal\Core\Config\Entity\ConfigEntityBundleBase;/*** @ConfigEntityType(* id = "config_pages_type",* admin_permission = "administer config_pages types",* label = @Translation("Config page type"),* config_prefix = "type",* entity_keys = {* "id" = "id",* "label" = "label",* "context" = "context",* "menu" = "menu"* },* )*/class ConfigPagesType extends ConfigEntityBundleBase implements ConfigPagesTypeInterface { /** * The config page type ID. */ protected $id; /** * The config page type label. */ protected $label;}
Listing of Config Entity
File: ConfigPagesType.php* handlers = {* "list_builder" = * "Drupal\config_pages\ConfigPagesTypeListBuilder"* },
File: config_pages.routing.ymlentity.config_pages_type.collection: path: '/admin/structure/config_pages/types' defaults: _entity_list: 'config_pages_type' _title: 'Config Pages Types' requirements: _permission: 'administer config_pages entity'
ConfigPagesTypeListBuilder class
<?phpnamespace Drupal\config_pages;use Drupal\Core\Config\Entity\ConfigEntityListBuilder;use Drupal\Core\Entity\EntityInterface;
/** * Defines a class to build a listing of custom config page type entities. */class ConfigPagesTypeListBuilder extends ConfigEntityListBuilder {
/** * Changes list of operation. */ public function getDefaultOperations(EntityInterface $entity) {...}
/** * Changes for header */ public function buildHeader() {...}
/** * Changes for row. */ public function buildRow(EntityInterface $entity) {...}
/** * {@inheritdoc} */ protected function getTitle() {...}}
Listing of ConfigPagesType objects
Path: /admin/structure/config_pages/types
Management forms of Config Entity
● Creating a new class ConfigPagesTypeForm, describing the form
● Add class to definition of Configuration Entity
● Add new route to config_pages.routing.yml
● Add new local action for adding new Configuration object
ConfigPagesTypeForm class
File: ConfigPagesTypeForm.php
namespace Drupal\config_pages;use Drupal\Core\Entity\EntityForm;use Drupal\Core\Entity\EntityTypeInterface;use Drupal\Core\Form\FormStateInterface;
/** * Base form for config_pages edit forms. */class ConfigPagesTypeForm extends EntityForm { /** * Required routes rebuild. */ protected $routesRebuildRequired = FALSE; /** * Form definition. */ public function form(array $form, FormStateInterface $form_state) {...} /** * Form validation. */ public function validateForm(array &$form, FormStateInterface $form_state) {...} /** * Form save. */ public function save(array $form, FormStateInterface $form_state) {...}}
Annotation of Configuration Entity
* handlers = {* "form" = {* "default" = "Drupal\config_pages\ConfigPagesTypeForm",* "add" = "Drupal\config_pages\ConfigPagesTypeForm",* "edit" = "Drupal\config_pages\ConfigPagesTypeForm",* "delete" = * "Drupal\config_pages\Form\ConfigPagesTypeDeleteForm"* },* "list_builder" = * "Drupal\config_pages\ConfigPagesTypeListBuilder"
* },
Routes with forms
File: config_pages.routing.yml
entity.config_pages_type.edit_form: path: '/admin/structure/config_pages/types/manage/{config_pages_type}' defaults: _entity_form: 'config_pages_type.edit' _title: 'Edit' requirements: _entity_access: 'config_pages_type.update' options: _admin_route: TRUE
entity.config_pages_type.delete_form: path: '/admin/structure/config_pages/types/manage/{config_pages_type}/delete' defaults: _entity_form: 'config_pages_type.delete' _title: 'Delete' requirements: _entity_access: 'config_pages_type.delete' options: _admin_route: TRUE
Edit Form example
Path: admin/structure/config_pages/types/manage/my_custom_page
Local action add
File: config_pages.links.action.yml
config_pages_type_add:
route_name: config_pages.type_add
title: 'Add config page'
appears_on:
- entity.config_pages_type.collection
Listing of configuration objects
Path: /admin/structure/config_pages/types
Configuration API Code examples
<?php
$config_name = 'config_pages.type.my_custom_page';
$config = \Drupal::service('config.factory') ->getEditable($config_name);// Instance of \Drupal\Core\Config\Config
$config = \Drupal::config($config_name);// Instance of \Drupal\Core\Config\ImmutableConfig
$object = Drupal\config_pages\Entity\ConfigPagesType::load('my_custom_page');
Thanks!