This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adrian Kozma
committed
Apr 26, 2018
0 parents
commit 83e69b3
Showing
6 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## Block field extra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: 'Block field extra' | ||
type: module | ||
description: 'Extends the block field functionality' | ||
core: 8.x | ||
package: 'block_field' | ||
dependencies: | ||
- block_field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains block_field_extra.module. | ||
*/ | ||
|
||
use Drupal\Core\Routing\RouteMatchInterface; | ||
|
||
/** | ||
* Implements hook_help(). | ||
*/ | ||
function block_field_extra_help($route_name, RouteMatchInterface $route_match) { | ||
switch ($route_name) { | ||
// Main module help for the block_field_extra module. | ||
case 'help.page.block_field_extra': | ||
$output = ''; | ||
$output .= '<h3>' . t('About') . '</h3>'; | ||
$output .= '<p>' . t('Extends the block field functionality') . '</p>'; | ||
return $output; | ||
|
||
default: | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Implements hook_preprocess(). | ||
*/ | ||
function block_field_extra_preprocess(&$variables, $hook, $info) { | ||
if ($hook = 'views_view') { | ||
if (isset($variables['view']->block_field_attachments)) { | ||
$variables['block_field_attachments'] = $variables['view']->block_field_attachments; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_views_data_alter(). | ||
*/ | ||
function block_field_extra_views_data_alter(array &$data) { | ||
$data['views']['block_field_attachments'] = [ | ||
'title' => t('Block field attachments'), | ||
'help' => t('Block field attachments'), | ||
'area' => [ | ||
'id' => 'block_field_attachments', | ||
], | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "WondrousLLC/block_field_extra", | ||
"type": "drupal-module", | ||
"description": "Extends the block field functionality", | ||
"keywords": ["Drupal"], | ||
"license": "GPL-2.0+", | ||
"homepage": "https://www.drupal.org/project/block_field_extra", | ||
"minimum-stability": "dev", | ||
"support": { | ||
"issues": "https://www.drupal.org/project/issues/block_field_extra", | ||
"source": "http://cgit.drupalcode.org/block_field_extra" | ||
}, | ||
"require": { | ||
"drupal/block_field": "^1.0-alpha5" | ||
} | ||
} |
199 changes: 199 additions & 0 deletions
199
src/Plugin/Field/FieldFormatter/BlockFieldAttachmentsFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
namespace Drupal\block_field_extra\Plugin\Field\FieldFormatter; | ||
|
||
use Drupal\block_field\Plugin\Field\FieldFormatter\BlockFieldFormatter; | ||
use Drupal\Component\Plugin\Exception\ContextException; | ||
use Drupal\Core\Field\FieldItemListInterface; | ||
use Drupal\Core\Field\FormatterBase; | ||
use Drupal\Core\Plugin\ContextAwarePluginInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Plugin implementation of the 'block_field_attachments' formatter. | ||
* | ||
* @FieldFormatter( | ||
* id = "block_field_attachments", | ||
* label = @Translation("Block field with attachments"), | ||
* field_types = { | ||
* "block_field" | ||
* } | ||
* ) | ||
*/ | ||
class BlockFieldAttachmentsFormatter extends BlockFieldFormatter { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) { | ||
$settings = $this->getSettings(); | ||
$entity = $items->getEntity(); | ||
foreach ($settings['field'] as $field_name) { | ||
if ($entity->hasField($field_name) && ($field_value = $this->getValue($entity, $field_name))) { | ||
$build = $this->getFieldBuild($entity, $field_name); | ||
$block_field_attachments[$field_name] = $build ?: $field_value; | ||
} | ||
} | ||
|
||
if (!empty($block_field_attachments)) { | ||
$block_field_attachments['#context'] = [ | ||
'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); | ||
} | ||
} | ||
|
||
// Let the parent to build the elements. | ||
// Calling the parent::viewElements will add by default the $attachment settings under | ||
// $element #configuration. | ||
// see $element[DELTA]['#configuration']['block_field_attachments'][FIELD_NAME] | ||
$elements = parent::viewElements($items, $langcode); | ||
|
||
if (isset($block_field_attachments)) { | ||
foreach ($elements as &$element) { | ||
$element['block_field_attachments'] = $block_field_attachments; | ||
if (isset($element['content']['#view'])) { | ||
$view = $element['content']['#view']; | ||
$view->block_field_attachments = $block_field_attachments; | ||
} | ||
} | ||
} | ||
|
||
return $elements; | ||
} | ||
|
||
|
||
public function getFieldBuild($entity, $field_name) { | ||
$build = $entity->{$field_name}->view($this->viewMode); | ||
|
||
return $build; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function defaultSettings() { | ||
return parent::defaultSettings() + [ | ||
'field' => 'field_attachment', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsForm(array $form, FormStateInterface $form_state) { | ||
$element = parent::settingsForm($form, $form_state); | ||
|
||
// Make sure if there's a view options to select. | ||
$options = $this->getFieldsList(); | ||
$element['field'] = [ | ||
'#title' => $this->t('Attachment field'), | ||
'#description' => $this->t('The selected fields will be injected into the block instance'), | ||
'#type' => 'select', | ||
'#multiple' => TRUE, | ||
'#options' => $this->getEntityProperties() + $options, | ||
'#default_value' => $this->getSetting('field'), | ||
]; | ||
|
||
return $element; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsSummary() { | ||
$summary = parent::settingsSummary(); | ||
return $summary; | ||
} | ||
|
||
/** | ||
* Get the the entity property or field value. | ||
* | ||
* @param $entity | ||
* The entity object. | ||
* @param $name | ||
* The property or field name. | ||
* | ||
* @return mixed | ||
* The value or FALSE. | ||
*/ | ||
public function getValue($entity, $name) { | ||
// Check if it's a field. | ||
if ($entity->hasField($name)) { | ||
$field_value = $entity->get($name)->getValue(); | ||
// TODO Uggly solution, find a better way to get the value property key. | ||
$value_keys = ['value', 'target_id']; | ||
foreach ($value_keys as $key) { | ||
if (isset($field_value[0][$key])) { | ||
return $field_value[0][$key]; | ||
} | ||
} | ||
} | ||
else { | ||
// Check for property. | ||
if (isset($entity->{$name})) { | ||
return $entity->{$name}->value; | ||
} | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
/** | ||
* Gets the list of the host entity properties. | ||
* | ||
* @return array | ||
* The list of available properties. | ||
*/ | ||
function getEntityProperties() { | ||
// TODO Get the properties dynamically. | ||
return ['id' => $this->t('Entity ID')]; | ||
} | ||
|
||
/** | ||
* Gets the list fields from the host entity. | ||
* | ||
* @return array | ||
* List of fields. | ||
*/ | ||
public function getFieldsList() { | ||
$fields = []; | ||
$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(); | ||
} | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
/** | ||
* Gets the field definitions of the host entity. | ||
* | ||
* @return array | ||
* The field definitions. | ||
*/ | ||
public function getFieldDefinitions() { | ||
$field_definition = $this->fieldDefinition; | ||
$entity_field_manager = \Drupal::service('entity_field.manager'); | ||
$entity_type = $field_definition->get('entity_type'); | ||
$bundle = $field_definition->get('bundle'); | ||
|
||
// Get the list fields from the host entity. | ||
$entity_field_definitions = $entity_field_manager->getFieldDefinitions($entity_type, $bundle); | ||
|
||
return $entity_field_definitions; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Drupal\block_field_extra\Plugin\views\area; | ||
|
||
use Drupal\views\Plugin\views\area\AreaPluginBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Link; | ||
|
||
/** | ||
* Views area block_field_attachments handler. | ||
* | ||
* @ingroup views_area_handlers | ||
* | ||
* @ViewsArea("block_field_attachments") | ||
*/ | ||
class BlockFieldAttachments extends AreaPluginBase { | ||
|
||
/** | ||
* The entity object | ||
* | ||
* @var object | ||
*/ | ||
public $entity = NULL; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function defineOptions() { | ||
$options = parent::defineOptions(); | ||
$options['title'] = ['default' => '']; | ||
$options['label_from_argument'] = ['default' => []]; | ||
$options['link_label'] = ['default' => 'See all']; | ||
$options['link_label_html_tag'] = ['default' => 'h2']; | ||
return $options; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildOptionsForm(&$form, FormStateInterface $form_state) { | ||
parent::buildOptionsForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function render($empty = FALSE) { | ||
return isset($this->view->block_field_attachments) ? $this->view->block_field_attachments : []; | ||
} | ||
|
||
} |