You are here

class FieldBlock in Field as Block 8

Same name in this branch
  1. 8 src/Plugin/Derivative/FieldBlock.php \Drupal\fieldblock\Plugin\Derivative\FieldBlock
  2. 8 src/Plugin/Block/FieldBlock.php \Drupal\fieldblock\Plugin\Block\FieldBlock

Provides block plugin definitions for fieldblock blocks.

Hierarchy

Expanded class hierarchy of FieldBlock

See also

\Drupal\fieldblock\Plugin\Block\FieldBlock

File

src/Plugin/Derivative/FieldBlock.php, line 22
Contains \Drupal\fieldblock\Plugin\Derivative\FieldBlock.

Namespace

Drupal\fieldblock\Plugin\Derivative
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
FieldBlock::$entityViewDisplayStorage protected property The entity view display storage.
FieldBlock::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
FieldBlock::fieldBlockGetBlockList protected function Builds a list of fields that have been made available as a block.
FieldBlock::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
FieldBlock::__construct public function Constructs a FieldBlock deriver object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.