diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php
index 5264850..82954f3 100644
--- a/src/Plugin/Block/EntityField.php
+++ b/src/Plugin/Block/EntityField.php
@@ -11,6 +11,7 @@ use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
@@ -118,77 +119,23 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont
}
/**
- * Gets the field definition.
- *
- * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
- */
- protected function getFieldDefinition() {
- if (empty($this->fieldDefinition)) {
- $field_map = $this->entityManager->getFieldMap();
- $bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']);
- $field_definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle);
- $this->fieldDefinition = $field_definitions[$this->fieldName];
- }
- return $this->fieldDefinition;
- }
-
- /**
- * Gets the field storage definition.
- *
- * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
+ * {@inheritdoc}
*/
- protected function getFieldStorageDefinition() {
- if (empty($this->fieldStorageDefinition)) {
- $field_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId);
- $this->fieldStorageDefinition = $field_definitions[$this->fieldName];
- }
- return $this->fieldStorageDefinition;
- }
+ public function build() {
+ /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
+ $entity = $this->getContextValue('entity');
- /**
- * Gets field type definition.
- *
- * @return array
- * The field type definition.
- */
- protected function getFieldTypeDefinition() {
- return $this->fieldTypeManager->getDefinition($this->getFieldStorageDefinition()->getType());
- }
+ if ($entity instanceof FieldableEntityInterface && $entity->hasField($this->fieldName)) {
+ /** @var \Drupal\Core\Field\FieldItemInterface $field */
+ $field = $entity->{$this->fieldName};
+ $display_settings = $this->getConfiguration()['formatter'];
+ $build['field'] = $field->view($display_settings);
- /**
- * Gets the formatter options for this field type.
- *
- * @return array
- * The formatter options.
- */
- protected function getFormatterOptions() {
- return $this->formatterManager->getOptions($this->getFieldStorageDefinition()->getType());
- }
+ CacheableMetadata::createFromObject($entity)
+ ->applyTo($build);
- /**
- * Gets the formatter object.
- *
- * @param string $type
- * The formatter name.
- * @param string $label
- * The label option for the formatter.
- * @param array $settings
- * The formatter settings.
- * @param array $third_party_settings
- * The formatter third party settings.
- *
- * @return \Drupal\Core\Field\FormatterInterface
- * The formatter object.
- */
- protected function getFormatter($type, $label, array $settings, array $third_party_settings) {
- return $this->formatterManager->createInstance($type, [
- 'field_definition' => $this->getFieldDefinition(),
- 'view_mode' => 'default',
- 'prepare' => TRUE,
- 'label' => $label,
- 'settings' => $settings,
- 'third_party_settings' => $third_party_settings,
- ]);
+ return $build;
+ }
}
/**
@@ -238,7 +185,7 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont
];
// Add the formatter settings to the form via AJAX.
- $form['#process'][] = array($this, 'layoutSettingsProcessCallback');
+ $form['#process'][] = array($this, 'formatterSettingsProcessCallback');
$form['formatter_settings_wrapper'] = array(
'#prefix' => '
',
'#suffix' => '
',
@@ -253,9 +200,9 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont
}
/**
- * Render API callback: builds the layout settings elements.
+ * Render API callback: builds the formatter settings elements.
*/
- public function layoutSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
+ public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
$config = $this->getConfiguration();
$parents_base = $element['#parents'];
$formatter_parent = array_merge($parents_base, ['formatter_type']);
@@ -294,30 +241,83 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
- $config = $this->getConfiguration();
- $config['formatter']['label'] = $form_state->getValue('formatter_label');
- $config['formatter']['type'] = $form_state->getValue('formatter_type');
- $config['formatter']['settings'] = $form_state->getValue('formatter_settings');
- $this->setConfiguration($config);
+ $this->configuration['formatter']['label'] = $form_state->getValue('formatter_label');
+ $this->configuration['formatter']['type'] = $form_state->getValue('formatter_type');
+ $this->configuration['formatter']['settings'] = $form_state->getValue('formatter_settings');
}
/**
- * {@inheritdoc}
+ * Gets the field definition.
+ *
+ * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
*/
- public function build() {
- /** @var $entity \Drupal\Core\Entity\EntityInterface */
- $entity = $this->getContextValue('entity');
+ protected function getFieldDefinition() {
+ if (empty($this->fieldDefinition)) {
+ $field_map = $this->entityManager->getFieldMap();
+ $bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']);
+ $field_definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle);
+ $this->fieldDefinition = $field_definitions[$this->fieldName];
+ }
+ return $this->fieldDefinition;
+ }
- /** @var $field \Drupal\Core\Field\FieldItemInterface */
- if ($field = $entity->{$this->fieldName}) {
- $display_settings = $this->getConfiguration()['formatter'];
- $build['field'] = $field->view($display_settings);
+ /**
+ * Gets the field storage definition.
+ *
+ * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
+ */
+ protected function getFieldStorageDefinition() {
+ if (empty($this->fieldStorageDefinition)) {
+ $field_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId);
+ $this->fieldStorageDefinition = $field_definitions[$this->fieldName];
+ }
+ return $this->fieldStorageDefinition;
+ }
- CacheableMetadata::createFromObject($entity)
- ->applyTo($build);
+ /**
+ * Gets field type definition.
+ *
+ * @return array
+ * The field type definition.
+ */
+ protected function getFieldTypeDefinition() {
+ return $this->fieldTypeManager->getDefinition($this->getFieldStorageDefinition()->getType());
+ }
- return $build;
- }
+ /**
+ * Gets the formatter options for this field type.
+ *
+ * @return array
+ * The formatter options.
+ */
+ protected function getFormatterOptions() {
+ return $this->formatterManager->getOptions($this->getFieldStorageDefinition()->getType());
+ }
+
+ /**
+ * Gets the formatter object.
+ *
+ * @param string $type
+ * The formatter name.
+ * @param string $label
+ * The label option for the formatter.
+ * @param array $settings
+ * The formatter settings.
+ * @param array $third_party_settings
+ * The formatter third party settings.
+ *
+ * @return \Drupal\Core\Field\FormatterInterface
+ * The formatter object.
+ */
+ protected function getFormatter($type, $label, array $settings, array $third_party_settings) {
+ return $this->formatterManager->createInstance($type, [
+ 'field_definition' => $this->getFieldDefinition(),
+ 'view_mode' => 'default',
+ 'prepare' => TRUE,
+ 'label' => $label,
+ 'settings' => $settings,
+ 'third_party_settings' => $third_party_settings,
+ ]);
}
}