Problem/Motivation
With the introduction of Third party plugins managed (via behavior_settings serialized property) Paragraph opened a new gate to hellish extensions, one of which is
styled_options →
. But, being paragraphs a tricky ones, they have a tricky relation with translations. Now, let's suppose i create a new StyleOption plugin from the module above, that Manages a Label of the paragraph which is supposed to be used in Table Of Contents (TOC) behavior. If this label is filled - paragraph will be included in this TOC, if not - it's skipped. Did all that, but the paragraphs does'n seem to want saving translated behavior_settings on each revision of paragraph there is, although system creates one for each translation.
Steps to reproduce
1. Instal style_options module
2. Create new custom_module, and create file src/Plugin/StyleOption/Toc.php file with this content:
<?php
namespace Drupal\style_options_extra\Plugin\StyleOption;
use Drupal\Core\Form\FormStateInterface;
use Drupal\style_options\Plugin\StyleOptionPluginBase;
/**
* Define the class attribute option plugin.
*
* @StyleOption(
* id = "toc",
* label = @Translation("Table of Contents")
* )
*/
class Toc extends StyleOptionPluginBase {
/**
* {@inheritDoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('TOC Label'),
'#default_value' => $this->getValue('label') ?? '',
'#description' => $this->t('Custom label that will appear in table of contents. Leave empty so this element will be skipped. At least one paragraph has to have TOC Label filled to generate scrolling element'),
];
return $form;
}
/**
* {@inheritDoc}
*/
public function build(array $build) {
return $build;
}
}
3. Save file, $ drush cr, Enable behaviors on your /admin/structure/paragraphs_type/ form.
4. Create a node type (or use page) with field_paragraphs in it that can use this paragraph_type.
5. Fill the paragraph_content and switch the above tab from "Content" to "Behavior" and fill the TOC Label with let's say (my primary lang is italian, english is the translated one).
6. Translate the page into english, by editing the above said behavior in , save.
7. The paragraph's behaviors will be changed in (eng) Label in both ITA / ENG translations. (or in ALL translations if there're more).
Proposed resolution
Only thing you should do is add
...
$fields['behavior_settings'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Behavior settings'))
->setDescription(t('The behavior plugin settings'))
->setRevisionable(TRUE)
->setTranslatable(TRUE) //<-------------- HERE !
->setDefaultValue(serialize([]));
...
into src/Entity/Paragraph.php baseFieldDefinitions() method.
Remaining tasks
Test my patch
User interface changes
none
API changes
dunno, none i think.
Data model changes
not a clue.