You are here

FieldBlock.php in Field as Block 8

File

src/Plugin/Derivative/FieldBlock.php
View source
<?php

/**
 * @file
 * Contains \Drupal\fieldblock\Plugin\Derivative\FieldBlock.
 */
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;

/**
 * Provides block plugin definitions for fieldblock blocks.
 *
 * @see \Drupal\fieldblock\Plugin\Block\FieldBlock
 */
class FieldBlock extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity view display storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityViewDisplayStorage;

  /**
   * Constructs a FieldBlock deriver object.
   *
   * @param \Drupal\Core\Entity\Entity\EntityViewDisplay $entity_view_display
   *   The entity view display storage.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct($entity_view_display, TranslationInterface $string_translation) {
    $this->entityViewDisplayStorage = $entity_view_display;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {

    /** @var EntityManagerInterface $entity_manager */
    $entity_manager = $container
      ->get('entity.manager');
    return new static($entity_manager
      ->getStorage('entity_view_display'), $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Builds a list of fields that have been made available as a block.
   *
   * @return string[]
   *   An array of fieldblocks in the form of fieldblock_id => admin label.
   */
  protected function fieldBlockGetBlockList() {
    $fieldblocks = array();

    // Get all EntityViewDisplay config entities and iterate over them.
    $entity_view_displays = $this->entityViewDisplayStorage
      ->loadMultiple();

    /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $entity_view_display */
    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

Namesort descending Description
FieldBlock Provides block plugin definitions for fieldblock blocks.