Merge branch 'master' of figureslibres.io:bachir/popsu-d9

This commit is contained in:
armansansd
2021-08-17 12:35:09 +02:00
16 changed files with 1032 additions and 209 deletions

View File

@@ -0,0 +1,6 @@
views.field.view_computed_themes_reference:
type: views_field
label: 'Computed Themes Reference'
views.field.view_computed_projets_reference:
type: views_field
label: 'Computed Projets Reference'

View File

@@ -0,0 +1,6 @@
name: Popsu Programme
type: module
description: misc plugins for popsu's programmes.
core_version_requirement: ^8.8 || ^9
package: Popsu
dependencies:

View File

@@ -0,0 +1,88 @@
<?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',
],
];
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Drupal\popsu_programme\Plugin\Field\FieldType;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\TypedData\ComputedItemListTrait;
// https://www.drupal.org/node/2112677
// https://www.cornel.co/article/entity-reference-computed-field-example-drupal
// https://www.caxy.com/blog/drupal-custom-form-and-computed-fields
class ComputedProjetsReferences extends EntityReferenceFieldItemList
{
use ComputedItemListTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(BaseFieldDefinition $definition, $name, TypedDataInterface $parent) {
parent::__construct($definition, $name, $parent);
$this->entityTypeManager = \Drupal::entityTypeManager();
}
/**
* Compute the values.
*/
protected function computeValue() {
// $query = \Drupal::entityQuery('node')
$entity = $this->getEntity();
$storage = \Drupal::service('entity_type.manager')->getStorage('node');
$query = $storage->getQuery()
->condition('status', 1)
->condition('type', 'projet')
->condition('field_programme', $entity->id());
$results = $query->execute();
if ($results) {
$nids = array_values($results);
$i = 0;
foreach ($nids as $key => $nid) {
$this->list[$i] = $this->createItem($i, $nid);
$i++;
}
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Drupal\popsu_programme\Plugin\Field\FieldType;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\TypedData\ComputedItemListTrait;
// https://www.drupal.org/node/2112677
// https://www.cornel.co/article/entity-reference-computed-field-example-drupal
// https://www.caxy.com/blog/drupal-custom-form-and-computed-fields
class ComputedThemesReferences extends EntityReferenceFieldItemList
{
use ComputedItemListTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(BaseFieldDefinition $definition, $name, TypedDataInterface $parent) {
parent::__construct($definition, $name, $parent);
$this->entityTypeManager = \Drupal::entityTypeManager();
}
/**
* Compute the values.
*/
protected function computeValue() {
// $query = \Drupal::entityQuery('node')
$entity = $this->getEntity();
$storage = \Drupal::service('entity_type.manager')->getStorage('node');
$query = $storage->getQuery()
->condition('status', 1)
->condition('type', 'theme')
->condition('field_programme', $entity->id());
$results = $query->execute();
if ($results) {
$nids = array_values($results);
$i = 0;
foreach ($nids as $key => $nid) {
$this->list[$i] = $this->createItem($i, $nid);
$i++;
}
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Drupal\popsu_programme\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;
/**
* A handler to provide proper displays for profile current company.
*
* @ingroup views_field_handlers
*
* @ViewsField("views_computed_projets_reference")
*/
class ViewsComputedProjetsReference extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$relationship_entities = $values->_relationship_entities;
$projets = null;
// First check the referenced entity.
if (isset($relationship_entities['node'])) {
$node = $relationship_entities['node'];
}
else {
$node = $values->_entity;
}
$type = get_class($node);
if ($type === 'Drupal\node\Entity\Node') {
$projets = $node->get('computed_projets_references')->getvalue();
}
return count($projets) . ' villes';
}
/**
* {@inheritdoc}
*/
public function query() {
// This function exists to override parent query function.
// Do nothing.
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Drupal\popsu_programme\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;
/**
* A handler to provide proper displays for profile current company.
*
* @ingroup views_field_handlers
*
* @ViewsField("views_computed_themes_reference")
*/
class ViewsComputedThemesReference extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$relationship_entities = $values->_relationship_entities;
$themes = null;
// First check the referenced entity.
if (isset($relationship_entities['node'])) {
$node = $relationship_entities['node'];
}
else {
$node = $values->_entity;
}
$type = get_class($node);
if ($type === 'Drupal\node\Entity\Node') {
$themes = $node->get('computed_themes_references')->getvalue();
}
return count($themes) . ' themes';
}
/**
* {@inheritdoc}
*/
public function query() {
// This function exists to override parent query function.
// Do nothing.
}
}