Aller au contenu principal

Drupal: rattaché une entité à un bundle

Drupal fournit des mecanismes permettant de creer une entité avec un bundle, par example "node". Dans certains projets, on a besoin de ratacher une entité de contenu à un bundle existant.


Nous disponsons d'une entité de contenu :

<?php

namespace Drupal\bookingsystem_autoecole\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;

/**
 * Defines the Bks autoecole heures entity.
 *
 * @ingroup bookingsystem_autoecole
 *
 * @ContentEntityType(
 *   id = "bks_autoecole_heures",
 *   label = @Translation("Bks autoecole heures"),
 *   handlers = {
 *     "storage" = "Drupal\bookingsystem_autoecole\BksAutoecoleHeuresStorage",
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\bookingsystem_autoecole\BksAutoecoleHeuresListBuilder",
 *     "views_data" = "Drupal\bookingsystem_autoecole\Entity\BksAutoecoleHeuresViewsData",
 *     "translation" = "Drupal\bookingsystem_autoecole\BksAutoecoleHeuresTranslationHandler",
 *
 *     "form" = {
 *       "default" = "Drupal\bookingsystem_autoecole\Form\BksAutoecoleHeuresForm",
 *       "add" = "Drupal\bookingsystem_autoecole\Form\BksAutoecoleHeuresForm",
 *       "edit" = "Drupal\bookingsystem_autoecole\Form\BksAutoecoleHeuresForm",
 *       "delete" = "Drupal\bookingsystem_autoecole\Form\BksAutoecoleHeuresDeleteForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\bookingsystem_autoecole\BksAutoecoleHeuresHtmlRouteProvider",
 *     },
 *     "access" = "Drupal\bookingsystem_autoecole\BksAutoecoleHeuresAccessControlHandler",
 *   },
 *   base_table = "bks_autoecole_heures",
 *   data_table = "bks_autoecole_heures_field_data",
 *   revision_table = "bks_autoecole_heures_revision",
 *   revision_data_table = "bks_autoecole_heures_field_revision",
 *   show_revision_ui = TRUE,
 *   translatable = TRUE,
 *   admin_permission = "administer bks autoecole heures entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "vid",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "published" = "status",
 *   },
 *   revision_metadata_keys = {
 *     "revision_user" = "revision_uid",
 *     "revision_created" = "revision_timestamp",
 *     "revision_log_message" = "revision_log"
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}",
 *     "add-form" = "/admin/structure/bks_autoecole_heures/add",
 *     "edit-form" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/edit",
 *     "delete-form" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/delete",
 *     "version-history" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/revisions",
 *     "revision" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/revisions/{bks_autoecole_heures_revision}/view",
 *     "revision_revert" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/revisions/{bks_autoecole_heures_revision}/revert",
 *     "revision_delete" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/revisions/{bks_autoecole_heures_revision}/delete",
 *     "translation_revert" = "/admin/structure/bks_autoecole_heures/{bks_autoecole_heures}/revisions/{bks_autoecole_heures_revision}/revert/{langcode}",
 *     "collection" = "/admin/structure/bks_autoecole_heures",
 *   },
 *   field_ui_base_route = "bks_autoecole_heures.settings"
 * )
 */
class BksAutoecoleHeures extends EditorialContentEntityBase implements BksAutoecoleHeuresInterface {
  
  use EntityChangedTrait;
  use EntityPublishedTrait;
  
  /**
   *
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += [
      'user_id' => \Drupal::currentUser()->id()
    ];
  }
  
  /**
   *
   * {@inheritdoc}
   */
  protected function urlRouteParameters($rel) {
    $uri_route_parameters = parent::urlRouteParameters($rel);
    
    if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
    }
    elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
    }
    
    return $uri_route_parameters;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    
    foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
      $translation = $this->getTranslation($langcode);
      
      // If no owner has been set explicitly, make the anonymous user the owner.
      if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }
    
    // If no revision author has been set explicitly,
    // make the bks_autoecole_heures owner the revision author.
    if (!$this->getRevisionUser()) {
      $this->setRevisionUserId($this->getOwnerId());
    }
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function getName() {
    return $this->get('name')->value;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this->get('user_id')->entity;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this->get('user_id')->target_id;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this->set('user_id', $uid);
    return $this;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this->set('user_id', $account->id());
    return $this;
  }
  
  public function getHours() {
    return (int) $this->get('heures')->value;
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    
    // Add the published field.
    $fields += static::publishedBaseFieldDefinitions($entity_type);
    
    $fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setDescription(t('The name of the Bks autoecole heures entity.'))->setRevisionable(TRUE)->setSettings([
      'max_length' => 50,
      'text_processing' => 0
    ])->setDefaultValue('')->setDisplayOptions('view', [
      'label' => 'above',
      'type' => 'string',
      'weight' => -4
    ])->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -4
    ])->setDisplayConfigurable('form', TRUE)->setDisplayConfigurable('view', TRUE)->setRequired(TRUE);
    
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Authored by'))->setDescription(t('The user ID of author of the Bks autoecole heures entity.'))->setRevisionable(TRUE)->setSetting('target_type', 'user')->setSetting('handler', 'default')->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0
    ])->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'autocomplete_type' => 'tags',
        'placeholder' => ''
      ]
    ])->setDisplayConfigurable('form', TRUE)->setDisplayConfigurable('view', TRUE);
    
    
    $fields['status']->setDescription(t('A boolean indicating whether the Bks autoecole heures is published.'))->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'weight' => -3
    ]);
    
    $fields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Created'))->setDescription(t('The time that the entity was created.'));
    
    $fields['changed'] = BaseFieldDefinition::create('changed')->setLabel(t('Changed'))->setDescription(t('The time that the entity was last edited.'));
    
    $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')->setLabel(t('Revision translation affected'))->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))->setReadOnly(TRUE)->setRevisionable(TRUE)->setTranslatable(TRUE);
    
    return $fields;
  }
  
}

Nous disponsons egalement de l'entite de configuration : "booking_config_type"

Pour faire ce rattachement, vous devez avoir le module devel_entity_updates activée. Nous allons proceder en 4 phase :

1 - Modification de la function presave
On declenche une erreur si l'on essaye d'ajouter une entité sans le type de configuration.

  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    // check if booking_config_type is define.
    if (!$this->get('booking_config_type')->target_id)
      throw "La valeur doit etre reatacher une une configuration";
  ...

2 - On ajoute un champs pour sauvegarder la valeur
    $fields['booking_config_type'] = BaseFieldDefinition::create('entity_reference')->setLabel('booking config type')->setSetting('target_type', 'booking_config_type')->setDisplayOptions('view', [])->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'placeholder' => ''
      ]
    ])->setDisplayConfigurable('form', TRUE)->setDisplayConfigurable('view', TRUE)->setSetting('handler', 'default');
Apres cette etape vous devez mettre à jour les entites :
vendor/bin/drush entup


3 - modification au niveau de l'annotation :

Rechercher la ligne "entity_keys", et ajouté une entrée :
"bundle" = "booking_config_type",

Rechercher la ligne "links" et modifier la ligne "add-form" 
"add-form" = "/admin/structure/booking_equipes/add/{booking_config_type}",
Ajouter y la ligne suivante :
"add-page" = "/admin/structure/bks_autoecole_heures/add",

Rechercher la ligne "field_ui_base_route" et modifier la valeur 
field_ui_base_route = "entity.booking_config_type.edit_form",
Ajouter cette ligne avant la fermeture de l'annotation :
bundle_entity_type = "booking_config_type",
Apres modifications vous devez mettre à jour les entites :
vendor/bin/drush entup
 
4 - modification des liens :
Dans le fichier : {moduleName}.links.menu.yml recherche le lien contenant : bks_autoecole_heures.settings
Supprimer ce lien :
bks_autoecole_heures.admin.structure.settings:
  title: 'Bks autoecole heures settings'
  description: 'Configure Bks autoecole heures entities'
  route_name: bks_autoecole_heures.settings
  parent: system.admin_structure
Rechercher egalement cette ligne dans {moduleName}.links.task.yml et supprimer le lien.
bks_autoecole_heures.settings_tab:
  route_name: bks_autoecole_heures.settings
  title: 'Settings'
  base_route: bks_autoecole_heures.settings
Dans le fichier : {moduleName}.links.action.yml remplace route_name: entity.bks_autoecole_heures.add_form par route_name: entity.bks_autoecole_heures.add_page.

 

Verification

Verifier la presence de la colonne 'booking_config_type' au niveaux des tables 'booking_equipes' et 'booking_equipes_field_data'. 
Stephane K
Il y'a 11 months
Modifié
Il y'a 11 months
Loading ...