Converting paragraphs
Last updated on
1 April 2024
#3043559: Implement paragraphs conversion introduced a framwork to convert paragraphs to a different type. The convert action can be enabled as a feature in the stable paragraphs widget settings on the form display page.
Conversions are written as plugin, no conversions exist by default as they depend on the specific paragraph types that are used on a page.
Example:
<?php
namespace Drupal\paragraphs_test\Plugin\paragraphs\Conversion;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsConversionBase;
/**
* Provides a Paragraphs conversion plugin.
*
* @ParagraphsConversion(
* id = "paragraphs_test_text_to_text_image",
* label = @Translation("Convert to Text Image (No form)"),
* source_type = "text",
* target_type = {"text_image"}
* )
*/
class ParagraphsTestTextToTextImage extends ParagraphsConversionBase {
/**
* {@inheritdoc}
*/
public function convert(array $settings, ParagraphInterface $original_paragraph, array $converted_paragraph = NULL) {
$text = $original_paragraph->get('field_text_demo')->value;
return [
[
'type' => 'text_image',
'field_text_demo' => [
'value' => $text,
],
],
];
}
}
Check the base class for more complex logic, plugins can also provide configuration options.
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion