You are here

public function ParagraphsFieldInstanceSettings::transform in Paragraphs 8

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

src/Plugin/migrate/process/ParagraphsFieldInstanceSettings.php, line 21

Class

ParagraphsFieldInstanceSettings
Configure field instance settings for paragraphs.

Namespace

Drupal\paragraphs\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $type = $row
    ->getSourceProperty('type');
  if ($type == 'paragraphs') {
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo('paragraph');
    $target_bundles = [];
    if (!empty($value['allowed_bundles'])) {
      $target_bundles = array_filter($value['allowed_bundles'], function ($a) {
        return $a != -1;
      });
      $value['handler_settings']['negate'] = 0;
      if (empty($target_bundles)) {
        $value['handler_settings']['target_bundles'] = NULL;
      }
      else {
        $value['handler_settings']['target_bundles'] = $target_bundles;
      }
      unset($value['allowed_bundles']);
    }
    if (!empty($value['bundle_weights'])) {

      // Copy the existing weights, and add any new bundles (either from
      // a field collection migration happening now, or pre-existing on the
      // site at the bottom.
      foreach ($value['bundle_weights'] as $bundle_name => $weight) {
        $value['handler_settings']['target_bundles_drag_drop'][$bundle_name] = [
          'enabled' => array_key_exists($bundle_name, $target_bundles),
          'weight' => $weight,
        ];
      }
      $other_bundles = array_keys(array_diff_key($bundles, $value['bundle_weights']));
      $weight = max($value['bundle_weights']);
      foreach ($other_bundles as $bundle_name) {
        $value['handler_settings']['target_bundles_drag_drop'][$bundle_name] = [
          'enabled' => array_key_exists($bundle_name, $target_bundles),
          'weight' => ++$weight,
        ];
      }
      unset($value['bundle_weights']);
    }
  }
  return $value;
}