diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php index 60007ce..346142a 100644 --- a/src/Plugin/Block/EntityField.php +++ b/src/Plugin/Block/EntityField.php @@ -7,6 +7,7 @@ namespace Drupal\ctools\Plugin\Block; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityManagerInterface; @@ -73,11 +74,18 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** * The field definition. * - * @var \Drupal\Core\Field\FieldStorageDefinitionInterface + * @var \Drupal\Core\Field\FieldDefinitionInterface */ protected $fieldDefinition; /** + * The field definition. + * + * @var \Drupal\Core\Field\FieldStorageDefinitionInterface + */ + protected $fieldStorageDefinition; + + /** * Constructs a new EntityField. * * @param array $configuration @@ -121,15 +129,30 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont * * @return \Drupal\Core\Field\FieldStorageDefinitionInterface */ - protected function getFieldStorageDefinition() { + protected function getFieldDefinition() { if (empty($this->fieldDefinition)) { - $field_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId); + $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; } /** + * Get 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; + } + + /** * Get field type. * * @return array @@ -150,6 +173,32 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont } /** + * Get 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, + ]); + } + + /** * {@inheritdoc} */ public function defaultConfiguration() { @@ -159,6 +208,7 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont 'label' => 'above', 'type' => $field_type_definition['default_formatter'] ?: '', 'settings' => [], + 'third_party_settings' => [], 'weight' => 0.0, ], ]; @@ -187,20 +237,74 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont '#title' => $this->t('Formatter'), '#options' => $this->getFormatterOptions(), '#default_value' => $form_state->getValue('formatter_type') ?: $config['formatter']['type'], + '#ajax' => array( + 'callback' => array(get_class($this), 'formatterSettingsAjaxCallback'), + 'wrapper' => 'formatter-settings-wrapper', + 'effect' => 'fade', + ), ]; - // @todo: use AJAX to provide the settings form. + // Add the formatter settings to the form via AJAX. + $form['#process'][] = array($this, 'layoutSettingsProcessCallback'); + $form['formatter_settings_wrapper'] = array( + '#prefix' => '