diff --git a/modules/ctools_block/config/schema/ctools_block.schema.yml b/modules/ctools_block/config/schema/ctools_block.schema.yml index e69de29..a65590d 100644 --- a/modules/ctools_block/config/schema/ctools_block.schema.yml +++ b/modules/ctools_block/config/schema/ctools_block.schema.yml @@ -0,0 +1,26 @@ +block.settings.entity_field:*:*: + type: block_settings + label: 'Entity field block' + mapping: + formatter: + type: mapping + label: 'Field formatter' + mapping: + type: + type: string + label: 'Format type machine name' + weight: + type: integer + label: 'Weight' + label: + type: string + label: 'Label setting machine name' + settings: + type: field.formatter.settings.[%parent.type] + label: 'Settings' + third_party_settings: + type: sequence + label: 'Third party settings' + sequence: + type: field.formatter.third_party.[%key] + diff --git a/modules/ctools_block/src/Plugin/Block/EntityField.php b/modules/ctools_block/src/Plugin/Block/EntityField.php index a3b4209..bef6c8f 100644 --- a/modules/ctools_block/src/Plugin/Block/EntityField.php +++ b/modules/ctools_block/src/Plugin/Block/EntityField.php @@ -146,6 +146,8 @@ public function build() { $field = $entity->{$this->fieldName}; $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); + // @todo Move this code to self::blockAccess() after resolving + // https://www.drupal.org/node/2635238. if ($access_handler->access($entity, 'view') && $access_handler->fieldAccess('view', $this->getFieldDefinition(), NULL, $field)) { $display_settings = $this->getConfiguration()['formatter']; $build['field'] = $field->view($display_settings); @@ -159,7 +161,7 @@ public function build() { } /** - * @inheritDoc + * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); @@ -177,7 +179,7 @@ public function defaultConfiguration() { 'type' => $field_type_definition['default_formatter'] ?: '', 'settings' => [], 'third_party_settings' => [], - 'weight' => 0.0, + 'weight' => 0, ], ]; } @@ -205,24 +207,24 @@ public function blockForm($form, FormStateInterface $form_state) { '#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'), + '#ajax' => [ + 'callback' => [static::class, 'formatterSettingsAjaxCallback'], 'wrapper' => 'formatter-settings-wrapper', 'effect' => 'fade', - ), + ], ]; // Add the formatter settings to the form via AJAX. - $form['#process'][] = array($this, 'formatterSettingsProcessCallback'); - $form['formatter_settings_wrapper'] = array( + $form['#process'][] = [$this, 'formatterSettingsProcessCallback']; + $form['formatter_settings_wrapper'] = [ '#prefix' => '
', '#suffix' => '
', - ); - $form['formatter_settings_wrapper']['formatter_settings'] = array( + ]; + $form['formatter_settings_wrapper']['formatter_settings'] = [ '#tree' => TRUE, // The settings from the formatter plugin will be added in the // ::formatterSettingsProcessCallback method. - ); + ]; return $form; } @@ -236,7 +238,7 @@ public function formatterSettingsProcessCallback(array &$element, FormStateInter $formatter_parent = array_merge($parents_base, ['formatter_type']); $formatter_settings_parent = array_merge($parents_base, ['formatter_settings']); - $settings_element =& $element['formatter_settings_wrapper']['formatter_settings']; + $settings_element = &$element['formatter_settings_wrapper']['formatter_settings']; // Set the #parents on the formatter_settings so they end up as a peer to // formatter_type. @@ -271,7 +273,9 @@ public static function formatterSettingsAjaxCallback(array $form, FormStateInter public function blockSubmit($form, FormStateInterface $form_state) { $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'); + // @todo Remove this manual cast after https://www.drupal.org/node/2635236 + // is resolved. + $this->configuration['formatter']['settings'] = (array) $form_state->getValue('formatter_settings'); } /** diff --git a/modules/ctools_block/src/Plugin/Deriver/EntityFieldDeriver.php b/modules/ctools_block/src/Plugin/Deriver/EntityFieldDeriver.php index b9a35bd..bd5649f 100644 --- a/modules/ctools_block/src/Plugin/Deriver/EntityFieldDeriver.php +++ b/modules/ctools_block/src/Plugin/Deriver/EntityFieldDeriver.php @@ -39,7 +39,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { // Set plugin definition for derivative. $derivative = $base_plugin_definition; - $derivative['category'] = $this->t('@entity fields', array('@entity' => $entity_type_labels[$entity_type_id])); + $derivative['category'] = $this->t('@entity fields', ['@entity' => $entity_type_labels[$entity_type_id]]); $derivative['admin_label'] = $admin_label; $derivative['context'] = [ 'entity' => new ContextDefinition('entity:' . $entity_type_id, $entity_type_labels[$entity_type_id], TRUE), diff --git a/modules/ctools_block/src/Tests/EntityFieldBlockTest.php b/modules/ctools_block/src/Tests/EntityFieldBlockTest.php index 3918942..063ba3f 100644 --- a/modules/ctools_block/src/Tests/EntityFieldBlockTest.php +++ b/modules/ctools_block/src/Tests/EntityFieldBlockTest.php @@ -18,16 +18,13 @@ class EntityFieldBlockTest extends WebTestBase { /** - * @todo Allow strict config schema once we figure out how to make it work - * without hurling exceptions at us. + * {@inheritdoc} */ - protected $strictConfigSchema = false; + public static $modules = ['ctools_block', 'ctools_block_field_test']; /** - * {@inheritdoc} + * @todo. */ - public static $modules = array('ctools_block', 'ctools_block_field_test'); - public function testBodyField() { $block = $this->drupalPlaceBlock('entity_field:node:body', [ 'formatter' => [ @@ -42,7 +39,10 @@ public function testBodyField() { $this->assertText($block->label()); } - function testImageField() { + /** + * @todo. + */ + public function testImageField() { $block = $this->drupalPlaceBlock('entity_field:node:field_ctools_block_image', [ 'formatter' => [ 'type' => 'image', @@ -56,7 +56,7 @@ function testImageField() { $this->assertText($block->label()); } - function testNodeBaseFields() { + public function testNodeBaseFields() { $block = $this->drupalPlaceBlock('entity_field:node:uid', [ 'formatter' => [ 'type' => 'entity', @@ -70,7 +70,10 @@ function testNodeBaseFields() { $this->assertText($block->label()); } - function testRenderCache() { + /** + * @todo. + */ + public function testRenderCache() { /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $view */ EntityViewDisplay::load('node.ctools_block_field_test.default') ->removeComponent('body')