From 152a7a3db82c1fd83039ca45b5bdce4b77eada41 Mon Sep 17 00:00:00 2001 From: Adrian Kozma Date: Thu, 26 Apr 2018 15:28:48 +0200 Subject: [PATCH] Refactors the code. --- block_field_extra.module | 1 - .../BlockFieldAttachmentsFormatter.php | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/block_field_extra.module b/block_field_extra.module index 9952f42..ccbc790 100644 --- a/block_field_extra.module +++ b/block_field_extra.module @@ -23,7 +23,6 @@ function block_field_extra_help($route_name, RouteMatchInterface $route_match) { } } - /** * Implements hook_preprocess(). */ diff --git a/src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php b/src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php index 24f2d69..bcb81f2 100644 --- a/src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php +++ b/src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php @@ -40,13 +40,14 @@ public function viewElements(FieldItemListInterface $items, $langcode) { 'field_definition' => $items->getFieldDefinition(), 'langcode' => $langcode, ]; - // Inject the attachment as the field item settings. - // This can be useful if you need this data on the field type plugin. - foreach ($items as $item) { - $settings = $item->settings; - $settings['block_field_attachments'] = $block_field_attachments; - $item->set('settings', $settings); - } + +// // Inject the attachment as the field item settings. +// // This can be useful if you need this data on the field type plugin. +// foreach ($items as $item) { +// $settings = $item->settings; +// $settings['block_field_attachments'] = $block_field_attachments; +// $item->set('settings', $settings); +// } } // Let the parent to build the elements. @@ -57,8 +58,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (isset($block_field_attachments)) { foreach ($elements as &$element) { + // Let the block theme know about the attachment. + // FYI, you can access this value on the block template. $element['block_field_attachments'] = $block_field_attachments; + if (isset($element['content']['#view'])) { + // Let the view know about the attachment. + // FYI, you can access this value on the view template. $view = $element['content']['#view']; $view->block_field_attachments = $block_field_attachments; } @@ -68,13 +74,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - - public function getFieldBuild($entity, $field_name) { - $build = $entity->{$field_name}->view($this->viewMode); - - return $build; - } - /** * {@inheritdoc} */ @@ -109,9 +108,26 @@ public function settingsForm(array $form, FormStateInterface $form_state) { */ public function settingsSummary() { $summary = parent::settingsSummary(); + // Todo implement the summary. return $summary; } + /** + * Gets the entity field build. + * + * @param $entity + * The entity object. + * @param $field_name + * The field name. + * + * @return mixed + */ + public function getFieldBuild($entity, $field_name) { + $build = $entity->{$field_name}->view($this->viewMode); + + return $build; + } + /** * Get the the entity property or field value. * @@ -167,7 +183,6 @@ public function getFieldsList() { $entity_field_definitions = $this->getFieldDefinitions(); $current_field = $this->fieldDefinition->getName(); - // Build the arguments array; foreach ($entity_field_definitions as $entity_field_definition) { if ($entity_field_definition instanceof \Drupal\field\Entity\FieldConfig && $entity_field_definition->getName() != $current_field ) { $fields[$entity_field_definition->getName()] = $entity_field_definition->label(); @@ -195,5 +210,4 @@ public function getFieldDefinitions() { return $entity_field_definitions; } - }