public function ParagraphsConversionManager::applyDefaultValues

Applies default values to a converted paragraph.

Parameters

\Drupal\paragraphs\ParagraphInterface $original_paragraph: The original paragraph.

\Drupal\paragraphs\ParagraphInterface $converted_paragraph: The converted paragraph.

File

paragraphs/src/ParagraphsConversionManager.php, line 157

Class

ParagraphsConversionManager
Plugin type manager for paragraphs type conversion plugins.

Namespace

Drupal\paragraphs

Code

public function applyDefaultValues(ParagraphInterface $original_paragraph, ParagraphInterface $converted_paragraph) {

  // Converted paragraph has to follow the language of the original paragraph.
  $converted_paragraph
    ->set($original_paragraph
    ->getEntityType()
    ->getKey('langcode'), $original_paragraph
    ->language()
    ->getId());

  // Converted paragraph should reuse the behavior settings when possible.
  foreach ($original_paragraph
    ->getAllBehaviorSettings() as $plugin_id => $settings) {
    if ($converted_paragraph
      ->getParagraphType()
      ->hasEnabledBehaviorPlugin($plugin_id)) {

      // Having the same plugin enabled is not enough. Some behavior plugins
      // provide configurability options. Compare their behavior plugin
      // configuration and in case they match, reuse the behavior settings.
      $original_plugin_configuration = $original_paragraph
        ->getParagraphType()
        ->getBehaviorPlugin($plugin_id)
        ->getConfiguration();
      $converted_plugin_configuration = $converted_paragraph
        ->getParagraphType()
        ->getBehaviorPlugin($plugin_id)
        ->getConfiguration();
      if ($original_plugin_configuration == $converted_plugin_configuration) {
        $converted_paragraph
          ->setBehaviorSettings($plugin_id, $settings);
      }
    }
  }
}