Custom entities in d8
-
Upload
samuel-solis-fuentes -
Category
Software
-
view
193 -
download
0
Transcript of Custom entities in d8
![Page 1: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/1.jpg)
Custom entities in D8
![Page 2: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/2.jpg)
Custom entities in D8
DrupalDay Santiago de compostela 2016
Samuel Solís (@estoyausente)
![Page 3: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/3.jpg)
@estoyausenteSamuel Solís
![Page 4: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/4.jpg)
Drupalday Santiago 2017 @estoyausente
![Page 5: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/5.jpg)
Drupalday Santiago 2017 @estoyausente
¿Qué vamos a ver? ¿Qué es un tipo de entidad? De contenido vs de configuración Algo de terminología Definición del tipo de entidad en bd Formularios Validaciones Renderizar entidades
![Page 6: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/6.jpg)
Drupalday Santiago 2017 @estoyausente
¿Qué es un tipo de entidad?
Un tipo de entidad es una clase que tiene métodos y manejadores definidos para almacenar, listar, cargar y mostrar una estructura de datos de forma unificada.
“
![Page 7: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/7.jpg)
Drupalday Santiago 2017 @estoyausente
¿Qué es un tipo de entidad?
Un tipo de entidad es una clase que tiene métodos y manejadores definidos para almacenar, listar, cargar y mostrar una estructura de datos de forma unificada.
“WTF????
![Page 8: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/8.jpg)
Drupalday Santiago 2017 @estoyausente
Config Content
EntitiesContent types
Views
Contact forms
Image styles
Nodes
Comments
Profiles
Terms
.yml files
CMI
![Page 9: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/9.jpg)
Drupalday Santiago 2017 @estoyausente
Config Content
EntitiesContent types
Views
Contact forms
Image styles
Nodes
Comments
Profiles
Terms
.yml files
CMI
![Page 10: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/10.jpg)
Drupalday Santiago 2017 @estoyausente
Algo de terminología
Bundle Field
PropertyEntity typenode author
Page Imagen
![Page 11: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/11.jpg)
Drupalday Santiago 2017 @estoyausente
![Page 12: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/12.jpg)
Drupalday Santiago 2017 @estoyausente
Definición del Tipo de Entidad
Anotación Definición de la clase método baseFieldDefinitions
![Page 13: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/13.jpg)
MiDrupal$
/** * @ContentEntityType( * id = "galician", * label = @Translation(“Galician"), * handlers = { * }, * }, * base_table = “galicians", * admin_permission = "administer galician people", * fieldable = FALSE, * entity_keys = { * "id" = "id", * }, * admin_permission = “administer galicians” * ) */
vim src/Entity/Galician.php
![Page 14: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/14.jpg)
MiDrupal$ vim src/Entity/MyEntity.php
namespace Drupal\galician\Entity; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Entity\ContentEntityBase; . . .
class Galician extends ContentEntityBase { use StringTranslationTrait;
}
vim src/Entity/Galician.php
![Page 15: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/15.jpg)
MiDrupal$ vim src/Entity/MyEntity.php
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer') ->setLabel(t('ID')) ->setDescription(t('The ID of the galician person.')) ->setReadOnly(TRUE);
$fields['clothing'] = BaseFieldDefinition::create('string') ->setLabel(t('From')) ->setDescription(t(‘Galician clothing.’)
->setSettings(array( 'default_value' => 'Raincoat',
)) ->setRequired(true);
$fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) ->setDescription(t('The time that the entity was created.'));
}
vim src/Entity/Galician.php
![Page 16: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/16.jpg)
MiDrupal$
$fields['province'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Province id.')) ->setDescription(t('The Pid of the associated province.')) ->setSetting('target_type', 'province') ->setSetting('handler', 'default');
vim src/Entity/Galician.php
![Page 17: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/17.jpg)
MiDrupal$
public function getClothing() {} public function setClothing() {} public function getOwner() {} public function preSave() {} public function postSave() {} public function preCreate() {} . . .
vim src/Entity/Galician.php
![Page 18: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/18.jpg)
MiDrupal$
public function eatPrawns($num) {
//eat a boat of prawns.
return true; }
vim src/Entity/Galician.php
![Page 19: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/19.jpg)
drush en miModulo
drush entity-updates
![Page 20: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/20.jpg)
MiDrupal$ vim any/other/Place.php
$galicianIds = \Drupal::entityQuery('galician') ->condition(‘clothing’,’raincoat' , '<>') ->sort('date', 'DESC') ->execute(); }
//Dirty and easy way $galicians = \Drupal::entityTypeManager() ->getStorage(‘galician')->loadMultiple($galicianIds);
//Dependency Injection way $galicians = $this->GalicianStorageController —>loadMultiple($orderIds);
foreach($galicians as $galician) { $galician->eatPrawns(100000); }
$galician->save();
![Page 21: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/21.jpg)
Drupalday Santiago 2017 @estoyausente
Formularios
![Page 22: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/22.jpg)
Drupalday Santiago 2017 @estoyausente
Formularios
![Page 23: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/23.jpg)
MiDrupal$
/** * @ContentEntityType( * id = "galician", * … some stuff * handlers = { * "form" = { * "add" = "Drupal\galicia\Form\GalicianForm", * "edit" = "Drupal\galicia\Form\GalicianForm", * "delete" = “Drupal\Core\Entity\ContentEntityDeleteForm", * }, * }, * }, * some stuff … * ) */
vim src/Entity/Galician.php
![Page 24: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/24.jpg)
MiDrupal$ vim src/Entity/MyEntity.php
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['clothing'] = BaseFieldDefinition::create('string') ->setLabel(t('From')) ->setDescription(t(‘Galician clothing.’)
->setSettings([( 'default_value' => 'Raincoat',
]) ->setDisplayConfigurable('Form', true) ->setDisplayOptions('Form', [ 'weigth' => 2, ])
->setRequired(true);
}
vim src/Entity/Galician.php
![Page 25: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/25.jpg)
MiDrupal$
namespace Drupal\galician\Form; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Form\FormStateInterface;
class GalicianForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); if(isSunny()) { // <— if(false) $form[‘clothing']['widget'][0]['default_value'] = ‘heat'; } return $form; }
public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state);
} }
vim src/Form/GalicianForm.php
![Page 26: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/26.jpg)
MiDrupal$
entity.galician.galician_add: path: '/galician/add' defaults: _entity_form: galician.add _title: ‘Create a new Galician person' requirements: _entity_create_access: 'galician'entity.galician.galician_add: path: '/galician/{galician}/edit' defaults: _entity_form: galician.edit _title: 'Edit Galician' requirements: _permission: 'administer galician entity'
vim galician.routing.yml
![Page 27: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/27.jpg)
Tia na naaaaa …
![Page 28: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/28.jpg)
Algunos gallegos creados …
![Page 29: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/29.jpg)
Algunos gallegos creados …
Mario Casas
![Page 30: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/30.jpg)
Rouco Varela
Algunos gallegos creados …
Mario Casas
![Page 31: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/31.jpg)
Rouco Varela
Algunos gallegos creados …
Mario Casas
Mariano Rajoy
![Page 32: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/32.jpg)
Rouco Varela
Mario Conde
Algunos gallegos creados …
Mario Casas
Mariano Rajoy
![Page 33: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/33.jpg)
Drupalday Santiago 2017 @estoyausente
Validaciones
Campos Entidad
![Page 34: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/34.jpg)
MiDrupal$ vim src/Entity/MyEntity.php
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['clothing'] = BaseFieldDefinition::create('string') ->setLabel(t('From')) ->setDescription($this->t('Galician clothing.')
->setSettings(array( 'default_value' => 'Raincoat',
)) ->addConstrain('Length', ['mas' => 20]) ->setRequired(true);
}
vim src/Entity/Galician.php
Campos
![Page 35: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/35.jpg)
MiDrupal$
* @ContentEntityType( * id = "galician", * constraints = { * "Galician" = {} * } * )
vim src/Entity/Galician.php
Entidad
![Page 36: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/36.jpg)
MiDrupal$
namespace Drupal\galician\Plugin\Validation\Constraint; use Symfony\Component\Validator\Constraint;
/** * Supports validating comment author names. * * @Constraint( * id = "Galician", * label = @Translation("Galician validation", context = "Validation"), * type = "entity:galician" * ) */ class GalicianConstraint extends Constraint { public $messageDate = ‘Date must be 12-11-2016’; public $messageClothing = ‘You aren\’t a real Galician!!’; }
vim src/Plugin/Validation/Contraint/GalicianConstraint.php
![Page 37: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/37.jpg)
MiDrupal$ vim src/Plugin/Validation/Contraint/GalicianConstraintValidator.php
namespace Drupal\galician\Plugin\Validation\Constraint; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator;
class GalicianConstraintValidator extends ConstraintValidator {
public function validate($entity, Constraint $constraint) {
if($entity->date != ’12-11-2016’) { $this->context->buildViolation($constraint->messageDate) ->addViolation(); }
if($entity->clothing != ‘raincoat’) { $this->context->buildViolation($constraint->messageClothing) ->addViolation(); } } }
![Page 38: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/38.jpg)
Drupalday Santiago 2017 @estoyausente
Renderizar entidades
Completas Listados
![Page 39: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/39.jpg)
MiDrupal$
$fields['clothing'] = BaseFieldDefinition::create('string') ->setLabel(t('From')) ->setDescription($this->t('Galician clothing.')
->addConstrain('Length', ['mas' => 20]) ->setRequired(true);
->setDisplayConfigurable('view', TRUE) ->setDisplayOptions('view', [ 'label' => 'above', 'type' => 'string', 'weight' => 1, ]);
vim src/Entity/Galician.php
Completas
![Page 40: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/40.jpg)
MiDrupal$
/** * @ContentEntityType( * id = "galician", * … some stuff * handlers = { * "list_builder” = “Drupal\galician\Controller\GalicianListBuilder”, * “view_builder” = “Drupal\Core\Entity\EntityViewBuilder” * }, * }, * some stuff … * ) */
vim src/Entity/Galician.php
Listados
![Page 41: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/41.jpg)
MiDrupal$ vim src/GalicianListBuilder.php
namespace Drupal\galician\Entity\Controller;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityListBuilder;
class GalicianListBuilder extends EntityListBuilder { public function render() {
$build['description'] = array( '#markup' => $this->t('Galician list.'), ); $build['table'] = parent::render(); return $build; }
public function buildHeader() { $header['id'] = $this->t('Order id'); $header['clothing'] = $this->t('Clothing'); return $header + parent::buildHeader(); }
public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['clothing'] = $entity->clothing->value; return $row + parent::buildRow($entity); } } Listados
![Page 42: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/42.jpg)
drush entity-updates
drush cr
![Page 43: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/43.jpg)
Tia na naaaaa …
![Page 44: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/44.jpg)
Algunos gallegos creados …
![Page 45: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/45.jpg)
Michel Salgado
Algunos gallegos creados …
![Page 46: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/46.jpg)
Rosalía de Castro
Michel Salgado
Algunos gallegos creados …
![Page 47: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/47.jpg)
Rosalía de Castro
Michel Salgado
Camilo José Cela
Algunos gallegos creados …
![Page 48: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/48.jpg)
Rosalía de Castro
Michel Salgado
Camilo José Cela
Agustín Jimenez
Algunos gallegos creados …
![Page 49: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/49.jpg)
Rosalía de Castro
Michel Salgado
Camilo José Cela
Algunos gallegos creados …
![Page 50: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/50.jpg)
Rosalía de Castro
Michel Salgado
Camilo José Cela Adolfo Dominguez
Algunos gallegos creados …
![Page 51: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/51.jpg)
Drupalday Santiago 2017 @estoyausente
Para otro día
Otros maneadores disponibles (storage, access, …) Bundles y fields Traducciones Revisiones Extensión de entidades existentes …
![Page 52: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/52.jpg)
Drupalday Santiago 2017 @estoyausente
Referencias
Core La documentación de
drupal.org Módulo Examples Drupal console (Aunque suele generar
bastante más de lo que necesitamos)Utilidad
![Page 53: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/53.jpg)
Drupalday Santiago 2017 @estoyausente
![Page 54: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/54.jpg)
Coffee break!
@estoyausenteSamuel Solís
Nos vemos en IRC #drupal-es
![Page 55: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/55.jpg)
![Page 56: Custom entities in d8](https://reader035.fdocuments.in/reader035/viewer/2022062408/58a4f4101a28abd8548b6cd9/html5/thumbnails/56.jpg)