12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?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, 30);
- foreach ($nids as $key => $nid) {
- $this->list[$key] = $this->createItem($key, $nid);
- }
- }
- }
|