popsu_programme.module 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. use Drupal\Core\Entity\EntityTypeInterface;
  3. use Drupal\Core\Field\BaseFieldDefinition;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. // https://www.drupal.org/project/drupal/issues/2916266#comment-12301574
  6. /**
  7. * Implement hook_entity_bundle_field_info().
  8. *
  9. * @param EntityTypeInterface $entity_type
  10. * @param $bundle
  11. * @param array $base_field_definitions
  12. * @return array
  13. */
  14. function popsu_programme_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  15. // function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  16. // $fields = array();
  17. // if ($entity_type->id() == 'node' && $bundle === 'frontpage') {
  18. // \Drupal::logger('materio_home')->notice('bundle: '.$bundle);
  19. if ($entity_type->id() == 'node' && $bundle == 'programme') {
  20. $fields['computed_themes_references'] = BaseFieldDefinition::create('entity_reference')
  21. ->setName('computed_themes_references')
  22. ->setLabel(t('Computed Themes References'))
  23. ->setDescription(t('Computed Themes References.'))
  24. // // The Entity Type this field belongs to.
  25. ->setTargetEntityTypeId($entity_type->id())
  26. // // The Entity Type bundle this field belongs to.
  27. ->setTargetBundle($bundle)
  28. ->setSetting('target_type', 'node')
  29. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  30. ->setComputed(TRUE)
  31. ->setRevisionable(FALSE)
  32. ->setTranslatable(FALSE)
  33. ->setDisplayConfigurable('view', TRUE)
  34. ->setDisplayOptions('view', [
  35. 'label' => 'hidden',
  36. 'weight' => -5,
  37. ])
  38. ->setClass(\Drupal\popsu_programme\Plugin\Field\FieldType\ComputedThemesReferences::class);
  39. $fields['computed_projets_references'] = BaseFieldDefinition::create('entity_reference')
  40. ->setName('computed_projets_references')
  41. ->setLabel(t('Computed Projets References'))
  42. ->setDescription(t('Computed Projets References.'))
  43. // // The Entity Type this field belongs to.
  44. ->setTargetEntityTypeId($entity_type->id())
  45. // // The Entity Type bundle this field belongs to.
  46. ->setTargetBundle($bundle)
  47. ->setSetting('target_type', 'node')
  48. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  49. ->setComputed(TRUE)
  50. ->setRevisionable(FALSE)
  51. ->setTranslatable(FALSE)
  52. ->setDisplayConfigurable('view', TRUE)
  53. ->setDisplayOptions('view', [
  54. 'label' => 'hidden',
  55. 'weight' => -5,
  56. ])
  57. ->setClass(\Drupal\popsu_programme\Plugin\Field\FieldType\ComputedProjetsReferences::class);
  58. return $fields;
  59. }
  60. }
  61. /**
  62. * Implements hook_views_data_alter().
  63. */
  64. function popsu_programme_views_data_alter(array &$data) {
  65. if (isset($data['node'])) {
  66. // Add the computed_themes_reference field to Views.
  67. $data['node']['computed_themes_reference'] = [
  68. 'title' => t('Computed Theme References'),
  69. 'field' => [
  70. 'id' => 'views_computed_themes_reference',
  71. ],
  72. ];
  73. // Add the computed_projets_reference field to Views.
  74. $data['node']['computed_projets_reference'] = [
  75. 'title' => t('Computed Projets References'),
  76. 'field' => [
  77. 'id' => 'views_computed_projets_reference',
  78. ],
  79. ];
  80. }
  81. }