first commit for custom module
This commit is contained in:
		| @@ -0,0 +1,3 @@ | ||||
| views.field.view_computed_date_unique: | ||||
|   type: views_field | ||||
|   label: 'Computed Date Unique' | ||||
| @@ -0,0 +1,52 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Drupal\popsu_uniqdate\Plugin\Field\FieldType; | ||||
| 
 | ||||
| use Drupal\Core\Entity\FieldableEntityInterface; | ||||
| // use Drupal\Core\Field\EntityReferenceFieldItemList;
 | ||||
| // use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
 | ||||
| use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; | ||||
| use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; | ||||
| use Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList; | ||||
| 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 ComputedUniqDate extends DateTimeFieldItemList | ||||
| { | ||||
|   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');
 | ||||
|     $dates = $entity->get('field_date')->getValue(); | ||||
|     asort($dates); | ||||
|     $last_date = array_pop($dates); | ||||
|     $this->list[0] = $this->createItem(0, $last_date); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
| @@ -0,0 +1,36 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Drupal\popsu_uniqdate\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_date_unique") | ||||
|  */ | ||||
| class ViewsComputedDateUnique extends FieldPluginBase { | ||||
| 
 | ||||
|   /** | ||||
|    * {@inheritdoc} | ||||
|    */ | ||||
|   public function render(ResultRow $values) { | ||||
|     $entity = $values->_entity; | ||||
|     $uniqdate = $entity->get('computed_date_unique')->getValue(); | ||||
|     return $uniqdate[0]['value']; | ||||
|     // $dates = $entity->get('field_date')->getvalue();
 | ||||
|     // asort($dates);
 | ||||
|     // return array_pop($dates);
 | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * {@inheritdoc} | ||||
|    */ | ||||
|   public function query() { | ||||
|     // This function exists to override parent query function.
 | ||||
|     // Do nothing.
 | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user