diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php index a50dbac..60007ce 100644 --- a/src/Plugin/Block/EntityField.php +++ b/src/Plugin/Block/EntityField.php @@ -8,6 +8,7 @@ namespace Drupal\ctools\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; @@ -98,9 +99,6 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont $this->entityTypeId = $entity_type_id; $this->fieldName = $field_name; - // Get list of bundles from the plugin definition. - $this->bundles = $plugin_definition['bundles']; - parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -157,8 +155,12 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont public function defaultConfiguration() { $field_type_definition = $this->getFieldTypeDefinition(); return [ - 'formatter' => $field_type_definition['default_formatter'] ?: '', - 'formatter_settings' => [], + 'formatter' => [ + 'label' => 'above', + 'type' => $field_type_definition['default_formatter'] ?: '', + 'settings' => [], + 'weight' => 0.0, + ], ]; } @@ -168,13 +170,27 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont public function blockForm($form, FormStateInterface $form_state) { $config = $this->getConfiguration(); - $form['formatter'] = [ + $form['formatter_label'] = [ + '#type' => 'select', + '#title' => $this->t('Label'), + '#options' => [ + 'above' => $this->t('Above'), + 'inline' => $this->t('Inline'), + 'hidden' => '- ' . $this->t('Hidden') . ' -', + 'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -', + ], + '#default_value' => $form_state->getValue('formatter_label') ?: $config['formatter']['label'], + ]; + + $form['formatter_type'] = [ '#type' => 'select', '#title' => $this->t('Formatter'), '#options' => $this->getFormatterOptions(), - '#default_value' => $form_state->getValue('formatter') ?: $config['formatter'], + '#default_value' => $form_state->getValue('formatter_type') ?: $config['formatter']['type'], ]; + // @todo: use AJAX to provide the settings form. + return $form; } @@ -182,6 +198,10 @@ 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'); + $this->setConfiguration($config); } /** @@ -191,15 +211,15 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** @var $entity \Drupal\Core\Entity\EntityInterface */ $entity = $this->getContextValue('entity'); - // Only attempt to do anything if this field is on this bundle. - if (!in_array($entity->bundle(), $this->bundles)) { - return []; - } - - // @todo Get this from configuration! - $display_settings = []; + /** @var $field \Drupal\Core\Field\FieldItemInterface */ if ($field = $entity->{$this->fieldName}) { - return $field->view($display_settings); + $display_settings = $this->getConfiguration()['formatter']; + $build['field'] = $field->view($display_settings); + + CacheableMetadata::createFromObject($entity) + ->applyTo($build); + + return $build; } } diff --git a/src/Plugin/Deriver/EntityFieldDeriver.php b/src/Plugin/Deriver/EntityFieldDeriver.php index 9820354..675edea 100644 --- a/src/Plugin/Deriver/EntityFieldDeriver.php +++ b/src/Plugin/Deriver/EntityFieldDeriver.php @@ -61,18 +61,25 @@ class EntityFieldDeriver extends DeriverBase implements ContainerDeriverInterfac $field_info = $entity_field_map[$field_name]; $derivative_id = $entity_type_id . ":" . $field_name; + // Get the admin label for both base and configurable fields. + if ($field_definition->isBaseField()) { + $admin_label = $field_definition->getLabel(); + } + else { + // We take the field label used on the first bundle. + $first_bundle = reset($field_info['bundles']); + $bundle_field_definitions = $this->entityManager->getFieldDefinitions($entity_type_id, $first_bundle); + $admin_label = $bundle_field_definitions[$field_name]->getLabel(); + } + // Set plugin definition for derivative. $derivative = $base_plugin_definition; $derivative['category'] = $this->t('@entity fields', array('@entity' => $entity_type_labels[$entity_type_id])); - $derivative['admin_label'] = $field_definition->getLabel(); + $derivative['admin_label'] = $admin_label; $derivative['context'] = [ 'entity' => new ContextDefinition('entity:' . $entity_type_id), ]; - // This isn't part of the Block plugin definition - we're just stashing - // it for use later. - $derivative['bundles'] = $field_info['bundles']; - $this->derivatives[$derivative_id] = $derivative; }