You are here

public function D6Webform::postImport in Webform: Migrate 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate/source/d6/D6Webform.php \Drupal\webform_migrate\Plugin\migrate\source\d6\D6Webform::postImport()

Performs post-import tasks.

Parameters

\Drupal\migrate\Event\MigrateImportEvent $event: The post-import event object.

Overrides ImportAwareInterface::postImport

File

src/Plugin/migrate/source/d6/D6Webform.php, line 613

Class

D6Webform
Drupal 6 webform source from database.

Namespace

Drupal\webform_migrate\Plugin\migrate\source\d6

Code

public function postImport(MigrateImportEvent $event) {

  // Add the Webform field to the webform content type
  // if it doesn't already exist.
  $field_storage = FieldStorageConfig::loadByName('node', 'webform');
  $field = FieldConfig::loadByName('node', 'webform', 'webform');
  if (empty($field)) {
    $field = \Drupal::service('entity_type.manager')
      ->getStorage('field_config')
      ->create([
      'field_storage' => $field_storage,
      'bundle' => 'webform',
      'label' => 'Webform',
      'settings' => [],
    ]);
    $field
      ->save();

    // Assign widget settings for the 'default' form mode.
    $display = \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', 'webform', 'default')
      ->getComponent('webform');
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', 'webform', 'default')
      ->setComponent('webform', [
      'type' => $display['type'],
    ])
      ->save();

    // Assign display settings for the 'default' and 'teaser' view modes.
    $display = \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'webform', 'default')
      ->getComponent('webform');
    \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'webform', 'default')
      ->setComponent('webform', [
      'label' => $display['label'],
      'type' => $display['type'],
    ])
      ->save();

    // The teaser view mode is created by the Standard profile and therefore
    // might not exist.
    $view_modes = \Drupal::service('entity_display.repository')
      ->getViewModes('node');
    if (isset($view_modes['teaser'])) {
      $display = \Drupal::service('entity_display.repository')
        ->getViewDisplay('node', 'webform', 'teaser')
        ->getComponent('webform');
      \Drupal::service('entity_display.repository')
        ->getViewDisplay('node', 'webform', 'teaser')
        ->setComponent('webform', [
        'label' => $display['label'],
        'type' => $display['type'],
      ])
        ->save();
    }
  }

  // Attach any Webform created to the relevant webforms if
  // Webform exists and Webform exists and Webform field is empty.
  $webforms = $this
    ->query()
    ->execute();
  foreach ($webforms as $webform) {
    $webform_nid = $webform['nid'];
    $webform_id = 'webform_' . $webform_nid;
    $webform = Webform::load($webform_id);
    if (!empty($webform)) {

      /** @var \Drupal\node\NodeInterface $node */
      $node = Node::load($webform_nid);
      if (!empty($node) && $node
        ->getType() == 'webform') {
        if (empty($node->webform->target_id)) {
          $node->webform->target_id = $webform_id;
          $node->webform->status = 1;
          $node
            ->save();
        }
      }
    }
  }
}