diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php index cc9d80c..a50dbac 100644 --- a/src/Plugin/Block/EntityField.php +++ b/src/Plugin/Block/EntityField.php @@ -9,6 +9,9 @@ namespace Drupal\ctools\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\Field\FieldTypePluginManagerInterface; +use Drupal\Core\Field\FormatterPluginManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; @@ -32,6 +35,20 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont protected $entityManager; /** + * The formatter manager. + * + * @var \Drupal\Core\Field\FieldTypePluginManagerInterface + */ + protected $fieldTypeManager; + + /** + * The formatter manager. + * + * @var \Drupal\Core\Field\FormatterPluginManager + */ + protected $formatterManager; + + /** * The entity type id. * * @var string @@ -55,7 +72,7 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** * The field definition. * - * @var \Drupal\Core\Field\FieldDefinitionInterface + * @var \Drupal\Core\Field\FieldStorageDefinitionInterface */ protected $fieldDefinition; @@ -71,10 +88,10 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) { $this->entityManager = $entity_manager; + $this->fieldTypeManager = $field_type_manager; + $this->formatterManager = $formatter_manager; // Get the entity type and field name from the plugin id. list ($plugin_base_id, $entity_type_id, $field_name) = explode(':', $plugin_id); @@ -83,6 +100,8 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont // Get list of bundles from the plugin definition. $this->bundles = $plugin_definition['bundles']; + + parent::__construct($configuration, $plugin_id, $plugin_definition); } /** @@ -93,36 +112,69 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('plugin.manager.field.field_type'), + $container->get('plugin.manager.field.formatter') ); } /** * Get the field definition. * - * @return \Drupal\Core\Field\FieldDefinitionInterface + * @return \Drupal\Core\Field\FieldStorageDefinitionInterface */ - protected function getFieldDefinition() { + protected function getFieldStorageDefinition() { if (empty($this->fieldDefinition)) { - $field_definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $this->bundles[0]); + $field_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId); $this->fieldDefinition = $field_definitions[$this->fieldName]; } return $this->fieldDefinition; } /** + * Get field type. + * + * @return array + * The field type definition. + */ + protected function getFieldTypeDefinition() { + return $this->fieldTypeManager->getDefinition($this->getFieldStorageDefinition()->getType()); + } + + /** + * Gets the formatter options for this field type. + * + * @return array + * The formatter options. + */ + protected function getFormatterOptions() { + return $this->formatterManager->getOptions($this->getFieldStorageDefinition()->getType()); + } + + /** * {@inheritdoc} */ public function defaultConfiguration() { - // @todo Why doesn't this work? - //return $this->getFieldDefinition()->getDisplayOptions('view'); - return []; + $field_type_definition = $this->getFieldTypeDefinition(); + return [ + 'formatter' => $field_type_definition['default_formatter'] ?: '', + 'formatter_settings' => [], + ]; } /** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { + $config = $this->getConfiguration(); + + $form['formatter'] = [ + '#type' => 'select', + '#title' => $this->t('Formatter'), + '#options' => $this->getFormatterOptions(), + '#default_value' => $form_state->getValue('formatter') ?: $config['formatter'], + ]; + return $form; } @@ -130,7 +182,6 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { - //$this->configuration['view_mode'] = $form_state->getValue('view_mode'); } /**