continue to build frontpage

This commit is contained in:
2019-05-24 13:07:50 +02:00
parent 72dca11b5e
commit c8829ee539
75 changed files with 613 additions and 110 deletions

View File

@@ -29,20 +29,21 @@ use Drupal\mymodule\Plugin\Field\FieldType\MyFieldComputed;
* @param array $base_field_definitions
* @return array
*/
// function materio_home_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
function materio_home_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') {
if ($entity_type->id() == 'node') {
// \Drupal::logger('materio_home')->notice('bundle: '.$bundle);
if ($entity_type->id() == 'node' && $bundle == 'frontpage') {
$fields['computed_materials_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_materials_reference')
->setLabel(t('Computed Materials References'))
->setDescription(t('Computed Materials References.'))
// // The Entity Type this field belongs to.
->setSetting('target_type', 'node')
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle('frontpage')
->setTargetEntityTypeId('node')
->setTargetBundle($bundle)
->setSetting('target_type', 'node')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
@@ -58,11 +59,11 @@ function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface
->setName('computed_showrooms_reference')
->setLabel(t('Computed Showrooms References'))
->setDescription(t('Computed Showrooms References.'))
->setSetting('target_type', 'taxonomy_term')
// // The Entity Type this field belongs to.
->setTargetEntityTypeId('node')
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle('frontpage')
->setTargetBundle($bundle)
->setSetting('target_type', 'taxonomy_term')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
@@ -73,8 +74,29 @@ function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface
'weight' => -5,
])
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedShowroomsReferences::class);
$fields['computed_articles_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_articles_reference')
->setLabel(t('Computed Articles References'))
->setDescription(t('Computed Articles 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\materio_home\Plugin\Field\FieldType\ComputedArticlesReferences::class);
return $fields;
}
return $fields;
}
// /**

View File

@@ -0,0 +1,49 @@
<?php
namespace Drupal\materio_home\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 ComputedArticlesReferences 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')
->condition('status', 1)
->sort('created', 'DESC')
->range(0,5)
->condition('type', 'article');
$nids = $query->execute();
foreach ($nids as $key => $nid) {
$this->list[$key] = $this->createItem($key, $nid);
}
}
}

View File

@@ -44,13 +44,8 @@ class ComputedMaterialsReferences extends EntityReferenceFieldItemList
->range(0,200);
$results = $query->execute();
$nids = array_rand($results, 20);
$nodes = entity_load_multiple('node', $nids);
// \Drupal::logger('materio_home')->notice(print_r($nodes, true));
$key = 0;
foreach ($nodes as $nid => $node) {
// \Drupal::logger('materio_home')->notice($nid);
$this->list[$key] = $this->createItem($key, $node->id());
$key++;
foreach ($nids as $key => $nid) {
$this->list[$key] = $this->createItem($key, $nid);
}
}

View File

@@ -40,13 +40,8 @@ class ComputedShowroomsReferences extends EntityReferenceFieldItemList
->condition('vid', 'showroom');
$tids = $query->execute();
shuffle($tids);
$terms = entity_load_multiple('taxonomy_term', $tids);
// \Drupal::logger('materio_home')->notice(print_r($nodes, true));
$key = 0;
foreach ($terms as $tid => $term) {
// \Drupal::logger('materio_home')->notice($nid);
$this->list[$key] = $this->createItem($key, $term->id());
$key++;
foreach ($tids as $key => $tid) {
$this->list[$key] = $this->createItem($key, $tid);
}
}