showing results for - "drupal 8 webform to node"
Alexandre
23 Oct 2018
1<?php
2
3/**
4 * Convert webform submission to "Bulletin" content type.
5 */
6
7namespace Drupal\subtonode\Controller;
8
9use Drupal\Core\Controller\ControllerBase;
10use Drupal\webform\Entity\WebformSubmission;
11use Drupal\node\Entity\Node;
12use Symfony\Component\HttpFoundation\RedirectResponse;
13
14class SubToNodeController extends ControllerBase {
15  public function subtonode($webform_submission) {
16    //$sid = 2;
17    $node_details = WebformSubmission::load($webform_submission);
18    $submission_array = $node_details->getOriginalData();
19    $title = $submission_array['title'];
20    $body = $submission_array['body'];
21    $contact_name = $submission_array['contact_name'];
22    $contact_email = $submission_array['contact_email'];
23    $contact_website_uri = $submission_array['website'];
24    $contact_website_title = $submission_array['website'];
25    $des_pub_date = $submission_array['bulletin_publish_date'];
26    $image_fid = $submission_array['image'];
27
28
29// Create file object from remote URL.
30    if (!empty($image_fid)) {
31      $file = \Drupal\file\Entity\File::load($image_fid);
32      $path = $file->getFileUri();
33      $data = file_get_contents($path);
34      $node_img_file = file_save_data($data, 'public://' . $file->getFilename(), FILE_EXISTS_REPLACE);
35    }
36
37    $timestamp = date("Y-m-d\TH:i:s", strtotime($des_pub_date));
38
39// Create node object with attached file.
40    $node = Node::create([
41      'type' => 'bulletin',
42      'title' => $title,
43      'body' => [
44        'value' => $body,
45        'summary' => '',
46        'format' => 'markdown',
47      ],
48      'field_bulletin_contact_name' => $contact_name,
49      'field_bulletin_contact_email' => $contact_email,
50      'field_bulletin_desired_publicati' => $timestamp,
51      'field_bulletin_reference_submiss' => [
52        'target_id' => $webform_submission,
53      ],
54      'field_bulletin_contact_website' => [
55        'uri' => $contact_website_uri,
56        'title' => $contact_website_title,
57      ],
58      'field_photo' => [
59        'target_id' => (!empty($node_img_file) ? $node_img_file->id() : NULL),
60        'alt' => 'Hello world',
61        'title' => 'Goodbye world'
62      ],
63    ]);
64
65    if (!empty($submission_array['audience'])) {
66      $target_ids_aud = $submission_array['audience'];
67      foreach ($target_ids_aud as $target_id) {
68        $node->field_bulletin_audience->AppendItem($target_id);
69      }
70    }
71
72    if (!empty($submission_array['category'])) {
73      $target_ids_cat = $submission_array['category'];
74      foreach ($target_ids_cat as $target_id) {
75        $node->field_bulletin_category->AppendItem($target_id);
76      }
77    }
78
79    $node->save();
80
81    $url = '/admin/content/webform';
82    $response = new RedirectResponse($url);
83    //$response->send(); // don't send the response yourself inside controller and form.
84
85    drupal_set_message(t('You have successfully created a node from webform submission @sid', array('@sid' => $webform_submission)), 'success');
86    return $response->send();
87  }
88}
89