0

I have a content block type, and I'd like to add a couple of simple configuration options to it without setting up "real" fields. I primarily used this for instructions:

https://www.webomelette.com/drupal-8-custom-data-configuration-entities-using-thirdpartysettingsinterface

My module code:

use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Form\FormStateInterface;

function oa_blocks_form_block_content_slider_images_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $block = $form_state->getFormObject()->getEntity();

  $form['autostart'] = [
    '#type' => 'checkbox',
    '#title' => t('Autostart'),
    '#default' => $block->getThirdPartySetting('oa_blocks', 'autostart'),
  ];
  $form['#entity_builders'][] = 'oa_blocks_block_content_slider_images_form_add_form_builder';
}

function oa_blocks_block_content_slider_images_form_add_form_builder($entity_type, BlockContent $block, &$form, FormStateInterface $form_state) {
  if ($form_state->getValue('autostart')) {
    $block->setThirdPartySetting('oa_blocks', 'autostart', $form_state->getValue('autostart'));
    return;
  }
  $block->unsetThirdPartySetting('oa_blocks', 'autostart');
}

I have the following in config/schema/oa_blocks.schema.yml:

block_content.type.slider_block.third_party.oa_blocks:
  type: mapping
  label: 'Slider Block'
  mapping:
    autostart:
      type: boolean
      label: 'Autostart'

I get the following error when I try to edit the block:

Fatal error: Call to undefined method Drupal\block_content\Entity\BlockContent::getThirdPartySetting() in /Users/dicksb/Sites/devdesktop/drupal8/docroot/profiles/mogov/modules/mogov/mogov_blocks/mogov_blocks.module on line 17

What am I doing wrong?

4

ThirdPartySettingsInterface is used by config entitiy types:

Interface for configuration entities to store third party information.

A third party is a module that needs to store tightly coupled information to the configuration entity. For example, a module alters the node type form can use this to store its configuration so that it will be deployed with the node type.

BlockContent is a content entity type, and doesn't implement the interface, hence the error.

  • Content entities have fields, so you can just add a field and store your value there – Berdir May 24 '17 at 19:05
  • I'm hoping to avoid that, as there'll ultimately be some 50-60 options. I will clearly need to rethink this. Perhaps I just need to build a block plugin instead. – WRD May 24 '17 at 19:27
  • 1
    If you have that many options then yes, plugins might be the better approach, yes. You could also use list fields with multiple values to group them into fewer fields. and the example looks slider related, the slick module provides re-usable slider presets that can be re-used, then you just have to select that. – Berdir May 24 '17 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.