FieldBlock.php in Field as Block 8
File
src/Plugin/Derivative/FieldBlock.php
View source
<?php
namespace Drupal\fieldblock\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldBlock extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityViewDisplayStorage;
public function __construct($entity_view_display, TranslationInterface $string_translation) {
$this->entityViewDisplayStorage = $entity_view_display;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
$entity_manager = $container
->get('entity.manager');
return new static($entity_manager
->getStorage('entity_view_display'), $container
->get('string_translation'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$blocks = $this
->fieldBlockGetBlockList();
foreach ($blocks as $fieldblock_id => $description) {
$this->derivatives[$fieldblock_id] = $base_plugin_definition;
$this->derivatives[$fieldblock_id]['admin_label'] = $description;
}
return $this->derivatives;
}
protected function fieldBlockGetBlockList() {
$fieldblocks = array();
$entity_view_displays = $this->entityViewDisplayStorage
->loadMultiple();
foreach ($entity_view_displays as $display_id => $entity_view_display) {
$view_display_fieldblocks = $entity_view_display
->getThirdPartySettings('fieldblock');
$entity_type = $entity_view_display
->get('targetEntityType');
$bundle = $entity_view_display
->get('bundle');
$mode = $entity_view_display
->get('mode');
foreach ($view_display_fieldblocks as $field_name => $field_label) {
$fieldblock_id = $display_id . ':' . $field_name;
$fieldblocks[$fieldblock_id] = $this
->t('@field field (from @type: @bundle: @mode)', array(
'@field' => $field_label,
'@type' => $entity_type,
'@bundle' => $bundle,
'@mode' => $mode,
));
}
}
return $fieldblocks;
}
}
Classes
Name |
Description |
FieldBlock |
Provides block plugin definitions for fieldblock blocks. |