created home node and some computed field

This commit is contained in:
2019-05-23 17:52:46 +02:00
parent 74a821104f
commit 5d2a8bc73b
31 changed files with 651 additions and 31 deletions

View File

@@ -28,6 +28,15 @@ class HomeController extends ControllerBase {
$contents = array("#theme"=>'materio_home');
// presentation
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('nid', 19990);
// TODO: présentation nid should be a setting
$pres_nid = $query->execute();
$contents["#frontpage_node"] = entity_load('node', array_pop($pres_nid));
return $contents;
}

View File

@@ -0,0 +1,57 @@
<?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 ComputedMaterialsReferences 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)
->condition('type', 'materiau')
->sort('created', 'DESC')
->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++;
}
}
}

View File

@@ -0,0 +1,53 @@
<?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 ComputedShowroomsReferences 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('taxonomy_term')
->condition('status', 1)
->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++;
}
}
}