1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?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;
- use Drupal\workflow\Entity\WorkflowManager;
- // 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');
- // remove masqué
- $tids = $query->execute();
- shuffle($tids);
- foreach ($tids as $key => $tid) {
- $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
- $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
- if($sid != 'workflow_visible') continue;
- $this->list[$key] = $this->createItem($key, $tid);
- }
- }
- }
|