Configuration entities
in Drupal 8.
Eugene Kulishov Adyax 2015
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: en
status: false
dependencies:
module:
- comment
id: comment.simple_comment
label: 'My simple comment'
targetEntityType: comment
cache: true
Config API code examples
<?php
// Get data from config.
$config = Drupal::config('system.site');
// Instance of DrupalCoreConfigImmutableConfig
$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 DrupalCoreConfigConfig
$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.yml
config_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.php
namespace Drupalconfig_pagesEntity;
use DrupalCoreConfigEntityConfigEntityBundleBase;
/**
* @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" =
* "Drupalconfig_pagesConfigPagesTypeListBuilder"
* },
File: config_pages.routing.yml
entity.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
<?php
namespace Drupalconfig_pages;
use DrupalCoreConfigEntityConfigEntityListBuilder;
use DrupalCoreEntityEntityInterface;
/**
* 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 Drupalconfig_pages;
use DrupalCoreEntityEntityForm;
use DrupalCoreEntityEntityTypeInterface;
use DrupalCoreFormFormStateInterface;
/**
* 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" = "Drupalconfig_pagesConfigPagesTypeForm",
* "add" = "Drupalconfig_pagesConfigPagesTypeForm",
* "edit" = "Drupalconfig_pagesConfigPagesTypeForm",
* "delete" =
* "Drupalconfig_pagesFormConfigPagesTypeDeleteForm"
* },
* "list_builder" =
* "Drupalconfig_pagesConfigPagesTypeListBuilder"
* },
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 DrupalCoreConfigConfig
$config = Drupal::config($config_name);
// Instance of DrupalCoreConfigImmutableConfig
$object =
Drupalconfig_pagesEntityConfigPagesType::load('my_custom
_page');
Thanks!

Configuration Entities in Drupal 8

  • 1.
    Configuration entities in Drupal8. Eugene Kulishov Adyax 2015
  • 2.
    What we willspeak 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
  • 3.
    Types of informationin Drupal 8 Content State Session Configuration Config API Configuration Entity API Status of modules Site name Content types Image styles
  • 4.
    Configuration entities inDrupal 8 ● Views ● Fields ● Content types ● Image styles ● Display settings ● Blocks ● Role ● Taxonomy vocabulary ● Date format ● Comment type ● Text format ● Date format
  • 5.
  • 6.
  • 7.
    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
  • 8.
    Configuration installation File: core.entity_view_mode.comment.simple_comment.yml langcode: en status:false dependencies: module: - comment id: comment.simple_comment label: 'My simple comment' targetEntityType: comment cache: true
  • 9.
    Config API codeexamples <?php // Get data from config. $config = Drupal::config('system.site'); // Instance of DrupalCoreConfigImmutableConfig $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 DrupalCoreConfigConfig $config->set('page.front', 'new-front-page'); $config->save();
  • 10.
    Main stages ofcreation ● Definition of scheme and interface ● Basic defenition of the class ● We expand opportunities: add listing of object, add CRUD forms ● Removing of configuration entity
  • 11.
    Schema of Configurationentity File: config_pages.schema.yml config_pages.type.*: type: config_entity label: 'Config page type settings' mapping: id: type: string label: 'ID' label: type: label label: 'Label'
  • 12.
    Basic definition ofthe class File: ConfigPagesType.php namespace Drupalconfig_pagesEntity; use DrupalCoreConfigEntityConfigEntityBundleBase; /** * @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; }
  • 13.
    Listing of ConfigEntity File: ConfigPagesType.php * handlers = { * "list_builder" = * "Drupalconfig_pagesConfigPagesTypeListBuilder" * }, File: config_pages.routing.yml entity.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'
  • 14.
    ConfigPagesTypeListBuilder class <?php namespace Drupalconfig_pages; useDrupalCoreConfigEntityConfigEntityListBuilder; use DrupalCoreEntityEntityInterface; /** * 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() {...} }
  • 15.
    Listing of ConfigPagesTypeobjects Path: /admin/structure/config_pages/types
  • 16.
    Management forms ofConfig 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
  • 17.
    ConfigPagesTypeForm class File: ConfigPagesTypeForm.php namespaceDrupalconfig_pages; use DrupalCoreEntityEntityForm; use DrupalCoreEntityEntityTypeInterface; use DrupalCoreFormFormStateInterface; /** * 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) {...} }
  • 18.
    Annotation of ConfigurationEntity * handlers = { * "form" = { * "default" = "Drupalconfig_pagesConfigPagesTypeForm", * "add" = "Drupalconfig_pagesConfigPagesTypeForm", * "edit" = "Drupalconfig_pagesConfigPagesTypeForm", * "delete" = * "Drupalconfig_pagesFormConfigPagesTypeDeleteForm" * }, * "list_builder" = * "Drupalconfig_pagesConfigPagesTypeListBuilder" * },
  • 19.
    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
  • 20.
    Edit Form example Path:admin/structure/config_pages/types/manage/my_custom_page
  • 21.
    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
  • 22.
    Listing of configurationobjects Path: /admin/structure/config_pages/types
  • 23.
    Configuration API Codeexamples <?php $config_name = 'config_pages.type.my_custom_page'; $config = Drupal::service('config.factory') ->getEditable($config_name); // Instance of DrupalCoreConfigConfig $config = Drupal::config($config_name); // Instance of DrupalCoreConfigImmutableConfig $object = Drupalconfig_pagesEntityConfigPagesType::load('my_custom _page');
  • 24.