Commit dc5330d4 authored by Sutharsan's avatar Sutharsan Committed by Sutharsan

Issue #2966274 by Sutharsan: Code style and documentation fixes

parent dfef2c69
......@@ -2,7 +2,6 @@
namespace Drupal\fieldblock;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
......@@ -55,7 +54,6 @@ class BlockEntityStorage extends ConfigEntityStorage {
* The language manager.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @throws PluginNotFoundException
*/
public function __construct(ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager) {
$this->configFactory = $config_factory;
......@@ -69,7 +67,8 @@ class BlockEntityStorage extends ConfigEntityStorage {
/**
* Load all blocks provided by this module.
*
* @return array|\Drupal\Core\Entity\EntityInterface[]
* @return \Drupal\Core\Entity\EntityInterface[]
* Block entities.
*/
public function loadFieldBlocks() {
// Build a query to fetch the entity IDs.
......@@ -82,15 +81,18 @@ class BlockEntityStorage extends ConfigEntityStorage {
/**
* Get all entity type ids that are currently used in Field Blocks.
*
* This will also return entity type ids for entities that no longer available.
* This will also return entity type ids for entities that are no longer
* available.
*
* @return array
* Entity type machine names.
*/
public function getEntityTypesUsed() {
$blocks = $this->loadFieldBlocks();
$entity_types = [];
/** @var \Drupal\block\Entity\Block $block */
foreach ($blocks as $block) {
$plugin_parts = explode(':',$block->get('plugin'));
$plugin_parts = explode(':', $block->get('plugin'));
$entity_types[] = $plugin_parts['1'];
}
......@@ -100,7 +102,8 @@ class BlockEntityStorage extends ConfigEntityStorage {
/**
* Delete all blocks for an entity type.
*
* @param $entity_type
* @param string $entity_type
* The entity type.
*/
public function deleteBlocksForEntityType($entity_type) {
$blocks = $this->loadByProperties(['plugin' => "fieldblock:$entity_type"]);
......
......@@ -12,11 +12,12 @@ use Drupal\Core\Entity\EntityTypeInterface;
* @package Drupal\fieldblock\Controller
*/
class FieldBlockController extends ControllerBase {
/**
* Get currently enabled types either from config or module defaults.
*
* @return array
* Entity type ids.
* Entity type ids.
*/
public function getEnabledEntityTypes() {
$entity_types = $this->config('fieldblock.settings')->get('enabled_entity_types');
......@@ -25,11 +26,15 @@ class FieldBlockController extends ControllerBase {
}
return array_filter($entity_types);
}
/**
* Determine if Entity Type should have field block created.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $type
* Entity type name.
*
* @return bool
* True if field block should be created.
*/
public function isBlockableEntityType(EntityTypeInterface $type) {
static $entity_types;
......@@ -43,13 +48,14 @@ class FieldBlockController extends ControllerBase {
* Return default entity types to use as blocks.
*
* @return array
* Entity type ids.
* Entity type ids.
*/
protected function getDefaultEntityTypes() {
$default_types = ['node', 'user', 'taxonomy_term'];
// @todo Should there by an alter hook to allow other modules to make their entities default?
$all_types = array_keys($this->entityManager()->getDefinitions());
// Return all default types that actually exist. "taxonomy_term" at least could be disabled.
// Return all default types that actually exist. "taxonomy_term" at least
// could be disabled.
return array_intersect($default_types, $all_types);
}
......@@ -57,8 +63,10 @@ class FieldBlockController extends ControllerBase {
* Determine if a Entity is compatible with Field Blocks.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type name.
*
* @return bool
* True if compatible.
*/
public function isFieldBlockCompatible(EntityTypeInterface $entity_type) {
return $entity_type instanceof ContentEntityTypeInterface;
......
......@@ -24,29 +24,39 @@ class FieldBlockConfigForm extends ConfigFormBase {
protected $entityManager;
/**
* The block entity storage.
*
* @var \Drupal\fieldblock\BlockEntityStorage
*/
protected $storage;
/**
* @var \Drupal\fieldblock\Controller\FieldBlockController;
* The field block controller.
*
* @var \Drupal\fieldblock\Controller\FieldBlockController
*/
protected $fieldblock_controller;
protected $fieldblockController;
/**
* Constructs a \Drupal\fieldblock\Form\FieldBlockConfigForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity type manager.
* @param \Drupal\fieldblock\BlockEntityStorage $storage
* The block entity storage.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, BlockEntityStorage $storage) {
parent::__construct($config_factory);
$this->entityManager = $entity_manager;
$this->storage = $storage;
$this->fieldblock_controller = new FieldBlockController();
$this->fieldblockController = new FieldBlockController();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
......@@ -55,13 +65,12 @@ class FieldBlockConfigForm extends ConfigFormBase {
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'fieldblock.settings'
'fieldblock.settings',
];
}
......@@ -76,7 +85,7 @@ class FieldBlockConfigForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$enabled = $this->fieldblock_controller->getEnabledEntityTypes();
$enabled = $this->fieldblockController->getEnabledEntityTypes();
$form['enabled_entity_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Enable Entity Types'),
......@@ -113,12 +122,16 @@ class FieldBlockConfigForm extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
/**
* Returns all entity types.
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
* Array of entity type definitions.
*/
protected function getAllEntityTypes() {
return array_keys($this->entityManager->getDefinitions());
}
/**
* {@inheritdoc}
*/
......@@ -133,7 +146,7 @@ class FieldBlockConfigForm extends ConfigFormBase {
parent::submitForm($form, $form_state);
$clear_cache = FALSE;
$previous_entity_types = $this->fieldblock_controller->getEnabledEntityTypes();
$previous_entity_types = $this->fieldblockController->getEnabledEntityTypes();
$new_entity_types = $form_state->getValue('enabled_entity_types');
if ($previous_entity_types != $new_entity_types) {
......@@ -174,13 +187,14 @@ class FieldBlockConfigForm extends ConfigFormBase {
* Get Entity Type labels for all compatible Entity Types.
*
* @return array
* Array of entity type labels keyed by the entity type machine name.
*/
protected function getEntityTypeLabels() {
$definitions = $this->entityManager->getDefinitions();
$labels = [];
/** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
foreach ($definitions as $definition) {
if ($this->fieldblock_controller->isFieldBlockCompatible($definition)) {
if ($this->fieldblockController->isFieldBlockCompatible($definition)) {
$labels[$definition->id()] = $definition->getLabel();
}
}
......@@ -188,17 +202,21 @@ class FieldBlockConfigForm extends ConfigFormBase {
}
/**
* Get orphaned entity types.
*
* Get all entity types that have Field Blocks but are either:
* 1. No longer set to be used with this module
* 2. Don't exist on the site.
*
* @param array $enabled_entity_types
* Currently enabled entity types.
* @return array Entity type ids.
*
* @return array
* Entity type ids.
*
* @todo param and return doc blocks must specify array of what, eg. string[].
*/
protected function getOrphanedEntityTypes($enabled_entity_types) {
protected function getOrphanedEntityTypes(array $enabled_entity_types) {
$orphaned_types = [];
$entity_types_used = $this->storage->getEntityTypesUsed();
$all_entity_types = $this->getAllEntityTypes();
......
......@@ -59,8 +59,14 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $fieldBlockEntity;
/**
* {@inheritdoc}
* Constructs a FieldBlock object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
......@@ -95,10 +101,16 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
'label_from_field' => TRUE,
'field_name' => '',
'formatter_id' => '',
'formatter_settings' => []
'formatter_settings' => [],
];
}
/**
* Returns field options.
*
* @return array
* Array of field option names keyed by their machine name.
*/
protected function getFieldOptions() {
$field_definitions = $this->entityManager->getFieldStorageDefinitions($this->getDerivativeId());
$options = [];
......@@ -108,6 +120,15 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
return $options;
}
/**
* Returns field formatter names.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field.
*
* @return array
* Array of formatter names keyed by field type.
*/
protected function getFormatterOptions(FieldDefinitionInterface $field_definition) {
$options = $this->formatterPluginManager->getOptions($field_definition->getType());
foreach ($options as $id => $label) {
......@@ -124,11 +145,12 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Gets the field definition.
*
* A FieldBlock works on an entity type across bundles, and thus only has access to
* field storage definitions. In order to be able to use formatters, we create a
* generic field definition out of that storage definition.
* A FieldBlock works on an entity type across bundles, and thus only has
* access to field storage definitions. In order to be able to use formatters,
* we create a generic field definition out of that storage definition.
*
* @param string $field_name
* The field name.
*
* @see BaseFieldDefinition::createFromFieldStorageDefinition()
* @see \Drupal\views\Plugin\views\field\Field::getFieldDefinition()
......@@ -145,6 +167,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* Gets the field storage definition.
*
* @param string $field_name
* The field name.
*
* @return \Drupal\field\FieldStorageConfigInterface
* The field storage definition used by this block.
......@@ -188,7 +211,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$form['formatter'] = [
'#type' => 'container',
'#id' => 'edit-block-formatter-wrapper'
'#id' => 'edit-block-formatter-wrapper',
];
$field_name = $form_state->getValue(['settings', 'field_name'], $this->configuration['field_name']);
......@@ -238,12 +261,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
'settings' => $formatter_settings,
'label' => '',
'weight' => 0,
]
],
];
if ($formatter_plugin = $this->formatterPluginManager->getInstance($formatter_options)) {
$formatter_settings_form = $formatter_plugin->settingsForm($form, $form_state);
// Convert field UI selector states to work in the block configuration form.
// Convert field UI selector states to work in the block configuration
// form.
FormHelper::rewriteStatesSelector($formatter_settings_form,
"fields[{$field_name}][settings_edit_form]",
'settings[formatter][settings]');
......@@ -262,7 +286,9 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* Element submit handler for non-JS field/formatter changes.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public static function blockFormChangeFieldOrFormatter(array $form, FormStateInterface $form_state) {
$form_state->setRebuild();
......@@ -271,12 +297,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Ajax callback on changing field_name or formatter_id form element.
*
* @param $form
* @param array $form
* The form.
*
* @return array
* The part of the form that has changed.
*/
public function blockFormChangeFieldOrFormatterAjax($form) {
public function blockFormChangeFieldOrFormatterAjax(array $form) {
return $form['settings']['formatter'];
}
......@@ -341,7 +368,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$build['field'] = $entity->get($this->configuration['field_name'])->view([
'label' => 'hidden',
'type' => $this->configuration['formatter_id'],
'settings' => $this->configuration['formatter_settings']
'settings' => $this->configuration['formatter_settings'],
]);
if ($this->configuration['label_from_field'] && !empty($build['field']['#title'])) {
$build['#title'] = $build['field']['#title'];
......@@ -366,8 +393,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* {@inheritdoc}
*/
public function getCacheContexts() {
// This block must be cached per route: every entity has its own canonical url
// and its own fields.
// This block must be cached per route: every entity has its own canonical
// url and its own fields.
return ['route'];
}
......@@ -376,6 +403,9 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
*
* @return \Drupal\Core\Entity\ContentEntityInterface|null
* The entity to be used when displaying the block.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getEntity() {
if (!isset($this->fieldBlockEntity)) {
......@@ -405,4 +435,5 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
}
return $this->fieldBlockEntity;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment