diff --git a/shareholder_register.install b/shareholder_register.install index 80914cfa4728e1c19c16327f924c420c3a5fb97f..e2d4b3238cdc11b05177ddcb03d67e7bb99aacb0 100644 --- a/shareholder_register.install +++ b/shareholder_register.install @@ -412,3 +412,29 @@ function shareholder_register_update_8017(&$sandbox) { ->installFieldStorageDefinition('shareholders', 'user', 'user', $storage_definition); } } + +/** + * Add shares field to shareholder. + */ +function shareholder_register_update_8018(&$sandbox) { + if (!array_key_exists( + 'shares', + \Drupal::service('entity.last_installed_schema.repository') + ->getLastInstalledFieldStorageDefinitions( + 'shareholder'))) { + $storage_definition = BaseFieldDefinition::create('shareholder_shares') + ->setLabel(t('Shares')) + ->setComputed(TRUE) + ->setSettings([ + 'target_type' => 'share', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => 15, + ]); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('shares', 'shareholder', 'shareholder', $storage_definition); + } +} diff --git a/src/Entity/Shareholder.php b/src/Entity/Shareholder.php index 4351b55ce2d7553958a7463117533a302435ca92..b9e80273fb2ad2e4f968a3820b5c74114511282c 100644 --- a/src/Entity/Shareholder.php +++ b/src/Entity/Shareholder.php @@ -430,7 +430,7 @@ class Shareholder extends RevisionableContentEntityBase implements ShareholderIn public function getDateDeceased() { return $this->get('deceased_date')->value; } - + /** * {@inheritdoc} */ @@ -760,6 +760,18 @@ class Shareholder extends RevisionableContentEntityBase implements ShareholderIn 'weight' => 11, ]); + $fields['shares'] = BaseFieldDefinition::create('shareholder_shares') + ->setLabel(t('Shares')) + ->setComputed(TRUE) + ->setSettings([ + 'target_type' => 'share', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => 15, + ]); + $fields['state'] = BaseFieldDefinition::create('string') ->setLabel(t('State')) ->setDescription(t('State of the Shareholder.')) diff --git a/src/Plugin/Field/FieldFormatter/SharesFormatter.php b/src/Plugin/Field/FieldFormatter/SharesFormatter.php index df04da716d2da83231c60b77e110a78f00a1d71c..47f692470ee576e3f4f7e43d602893eaf6c9d152 100644 --- a/src/Plugin/Field/FieldFormatter/SharesFormatter.php +++ b/src/Plugin/Field/FieldFormatter/SharesFormatter.php @@ -17,6 +17,7 @@ use Drupal\shareholder_register\Entity\Share; * label = @Translation("Shares formatter"), * field_types = { * "share_transaction_shares", + * "shareholder_shares", * } * ) */ diff --git a/src/Plugin/Field/FieldFormatter/SharesSummaryFormatter.php b/src/Plugin/Field/FieldFormatter/SharesSummaryFormatter.php new file mode 100644 index 0000000000000000000000000000000000000000..8b5ed64739eb054b74f4c64d48f9961af225f746 --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/SharesSummaryFormatter.php @@ -0,0 +1,89 @@ +<?php + +namespace Drupal\shareholder_register\Plugin\Field\FieldFormatter; + +use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Form\FormStateInterface; + +use Drupal\shareholder_register\Entity\Share; + +/** + * Plugin implementation of the 'share_transactions_formatter' formatter. + * + * @FieldFormatter( + * id = "shares_summary_formatter", + * label = @Translation("Shares Summary formatter"), + * field_types = { + * "share_transaction_shares", + * "shareholder_shares", + * } + * ) + */ +class SharesSummaryFormatter extends FormatterBase { + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return [ + // Implement default settings. + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + return [ + // Implement settings form. + ] + parent::settingsForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + // Implement settings summary. + return $summary; + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $share_ids = []; + foreach ($items as $item) { + $value = $item->getValue(); + if (isset($value['entity'])) { + $share_ids[] = $value['entity']->id(); + } + else { + $share_ids[] = $value['target_id']; + } + } + $groups = \Drupal::service('shareholder_register.default')->groupShareIdsByHash($share_ids); + + $elements[0] = [ + '#type' => 'table', + '#title' => $this->t('Shares'), + '#header' => [ + $this->t('Count'), + $this->t('Share numbers'), + $this->t('Hash'), + ], + ]; + foreach ($groups as $delta => $group) { + $elements[0]['#rows'][$delta] = [ + 'count' => $group['count'], + 'share_numbers' => $group['share_numbers'], + 'hash' => $group['hash_text'], + ]; + } + + return $elements; + } + +} diff --git a/src/Plugin/Field/FieldType/ShareholderShares.php b/src/Plugin/Field/FieldType/ShareholderShares.php new file mode 100644 index 0000000000000000000000000000000000000000..74d60a750b50e19fbbaf4613fbf1f52101a29e21 --- /dev/null +++ b/src/Plugin/Field/FieldType/ShareholderShares.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\shareholder_register\Plugin\Field\FieldType; + +use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; + +/** + * Plugin implementation of the 'share_transaction_shares' field type. + * + * @FieldType( + * id = "shareholder_shares", + * label = @Translation("Shareholder Shares"), + * description = @Translation("Shareholder Shares"), + * list_class = "\Drupal\shareholder_register\Plugin\Field\FieldType\ShareholderSharesItemList" + * ) + */ +class ShareholderShares extends EntityReferenceItem { + +} diff --git a/src/Plugin/Field/FieldType/ShareholderSharesItemList.php b/src/Plugin/Field/FieldType/ShareholderSharesItemList.php new file mode 100644 index 0000000000000000000000000000000000000000..454af71647f44228d83722427a1bfbbea342d371 --- /dev/null +++ b/src/Plugin/Field/FieldType/ShareholderSharesItemList.php @@ -0,0 +1,55 @@ +<?php + +namespace Drupal\shareholder_register\Plugin\Field\FieldType; + +use Drupal\Core\Field\EntityReferenceFieldItemList; +use Drupal\Core\TypedData\ComputedItemListTrait; + +use Drupal\shareholder_register\Entity\Share; + +/** + * {@inheritdoc} + */ +class ShareholderSharesItemList extends EntityReferenceFieldItemList { + use ComputedItemListTrait; + + /** + * Compute the values. + */ + protected function computeValue() { + $entity = $this->getEntity(); + + $values = Share::loadMultiple( + \Drupal::entityQuery('share') + ->accessCheck(FALSE) + ->condition('shareholder_id', $entity->id()) + ->execute() + ); + + if ($values) { + $n = 0; + foreach ($values as $delta => $value) { + $this->list[$n] = $this->createItem($delta, $value); + $n++; + } + } + } + + /** + * {@inheritdoc} + */ + public function referencedEntities() { + $this->ensureComputedValue(); + return parent::referencedEntities(); + } + + /** + * {@inheritdoc} + */ + public function getReferencedEntities() { + // Duplicate method, with prefix get for use in twig templates. + $this->valueComputed = FALSE; + return $this->referencedEntities(); + } + +}