Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Refactors the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Kozma committed Apr 26, 2018
1 parent 83e69b3 commit 152a7a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
1 change: 0 additions & 1 deletion block_field_extra.module
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function block_field_extra_help($route_name, RouteMatchInterface $route_match) {
}
}


/**
* Implements hook_preprocess().
*/
Expand Down
46 changes: 30 additions & 16 deletions src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -195,5 +210,4 @@ public function getFieldDefinitions() {
return $entity_field_definitions;
}


}

0 comments on commit 152a7a3

Please sign in to comment.