<?php
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

// https://www.drupal.org/project/drupal/issues/2916266#comment-12301574


/**
 * Implement hook_entity_bundle_field_info().
 *
 * @param EntityTypeInterface $entity_type
 * @param $bundle
 * @param array $base_field_definitions
 * @return array
 */
function popsu_programme_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
// function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  // $fields = array();
  // if ($entity_type->id() == 'node' && $bundle === 'frontpage') {
  // \Drupal::logger('materio_home')->notice('bundle: '.$bundle);
  if ($entity_type->id() == 'node' && $bundle == 'programme') {
    $fields['computed_themes_references'] = BaseFieldDefinition::create('entity_reference')
      ->setName('computed_themes_references')
      ->setLabel(t('Computed Themes References'))
      ->setDescription(t('Computed Themes References.'))
      // // The Entity Type this field belongs to.
      ->setTargetEntityTypeId($entity_type->id())
      // // The Entity Type bundle this field belongs to.
      ->setTargetBundle($bundle)
      ->setSetting('target_type', 'node')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setComputed(TRUE)
      ->setRevisionable(FALSE)
      ->setTranslatable(FALSE)
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
          'label' => 'hidden',
          'weight' => -5,
        ])
      ->setClass(\Drupal\popsu_programme\Plugin\Field\FieldType\ComputedThemesReferences::class);

    $fields['computed_projets_references'] = BaseFieldDefinition::create('entity_reference')
      ->setName('computed_projets_references')
      ->setLabel(t('Computed Projets References'))
      ->setDescription(t('Computed Projets References.'))
      // // The Entity Type this field belongs to.
      ->setTargetEntityTypeId($entity_type->id())
      // // The Entity Type bundle this field belongs to.
      ->setTargetBundle($bundle)
      ->setSetting('target_type', 'node')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setComputed(TRUE)
      ->setRevisionable(FALSE)
      ->setTranslatable(FALSE)
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
          'label' => 'hidden',
          'weight' => -5,
        ])
      ->setClass(\Drupal\popsu_programme\Plugin\Field\FieldType\ComputedProjetsReferences::class);

      return $fields;
  }
}


/**
 * Implements hook_views_data_alter().
 */
function popsu_programme_views_data_alter(array &$data) {
  if (isset($data['node'])) {
    // Add the computed_themes_reference field to Views.
    $data['node']['computed_themes_reference'] = [
      'title' => t('Computed Theme References'),
      'field' => [
        'id' => 'views_computed_themes_reference',
      ],
    ];
    // Add the computed_projets_reference field to Views.
    $data['node']['computed_projets_reference'] = [
      'title' => t('Computed Projets References'),
      'field' => [
        'id' => 'views_computed_projets_reference',
      ],
    ];
  }
}