diff --git a/src/Entity/BondTransactionViewsData.php b/src/Entity/BondTransactionViewsData.php index f7b5db3f08e9cf44fa127e546277f31997fe84de..20ef159613cc6d5f44b2e5db6763584364b9fa15 100644 --- a/src/Entity/BondTransactionViewsData.php +++ b/src/Entity/BondTransactionViewsData.php @@ -27,6 +27,14 @@ class BondTransactionViewsData extends EntityViewsData { 'label' => 'Related bonds', ], ]; + $data['bond_transaction']['bonds_field'] = [ + 'title' => $this->t('Bonds field'), + 'help' => $this->t('Displays the bonds attached to the entity.'), + 'field' => [ + 'id' => 'bonds_field', + ], + ]; + return $data; } diff --git a/src/Plugin/views/field/BondsField.php b/src/Plugin/views/field/BondsField.php new file mode 100644 index 0000000000000000000000000000000000000000..89c4093205333a5626ebcf6169136135db5ab127 --- /dev/null +++ b/src/Plugin/views/field/BondsField.php @@ -0,0 +1,54 @@ +<?php + +namespace Drupal\shareholder_register_bonds\Plugin\views\field; + +use Drupal\views\Plugin\views\field\FieldPluginBase; +use Drupal\views\ResultRow; + +/** + * A handler to provide a field that is completely custom by the administrator. + * + * @ingroup views_field_handlers + * + * @ViewsField("bonds_field") + */ +class BondsField extends FieldPluginBase { + + /** + * {@inheritdoc} + */ + public function usesGroupBy() { + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function query() { + // Do nothing -- to override the parent query. + } + + /** + * {@inheritdoc} + */ + protected function defineOptions() { + $options = parent::defineOptions(); + + $options['hide_alter_empty'] = ['default' => FALSE]; + return $options; + } + + /** + * {@inheritdoc} + */ + public function render(ResultRow $values) { + $entity = $this->getEntity($values); + return [ + '#markup' => implode(', ', array_map(function ($entity) {return $entity->label();}, $entity->get('bonds')->referencedEntities())), + '#cache' => [ + 'tags' => \Drupal::entityTypeManager()->getDefinition('bond_transaction')->getListCacheTags(), + ], + ]; + } + +}