willowf's picture

Hello everyone, I have a question about creating nodes in Backdrop CMS. Currently, users need to access the node creation form to publish content, but I would like to know if there is an option or module that allows creating a node directly from a form embedded in a block, without needing to access the standard form.

I have explored some alternatives:

  1. Webform: I couldn’t find a module that allows creating nodes directly through a Webform.
  2. Rules: I tried using Rules to achieve this, but I encountered an error because the Webform Rules module is obsolete and not compatible with my version of Backdrop CMS.
  3. formblock: Update 11 years ago 

Is there a recommended module or method to accomplish this? I would appreciate any suggestions or examples of how to implement it.

Comments

<?php

/**
 * Implements hook_webform_submission_insert().
 *
 * Triggered after a webform submission is inserted.
 */
function webform_to_node_webform_submission_insert($node, $submission) {
  // Check if this is the correct webform node.
  if ($node->nid == 33724) {
    // Access the submission data.
    $data = $submission->data;

    // Map the component IDs to meaningful keys (form_key).
    $mapped_data = webform_to_node_map_submission_data($node, $data);

    // Process the submission to create a node.
    webform_to_node_process_submission($mapped_data);
  }
}

/**
 * Map submission data using the form keys from the webform components.
 *
 * @param object $node
 *   The webform node.
 * @param array $data
 *   The raw submission data.
 *
 * @return array
 *   The mapped submission data with form keys as array keys.
 */
function webform_to_node_map_submission_data($node, $data) {
  $mapped_data = [];

  // Get the webform components.
  $components = $node->webform['components'];

  // Map the submission data using the form keys.
  foreach ($components as $cid => $component) {
    $form_key = $component['form_key'];
    $mapped_data[$form_key] = isset($data[$cid][0]) ? $data[$cid][0] : NULL;
  }

  return $mapped_data;
}

/**
 * Process the webform submission to create a node.
 *
 * @param array $data
 *   The mapped submission data.
 */
function webform_to_node_process_submission($data) {
  $node = new Node();
  $node->type = 'biography';
  $node->status = NODE_NOT_PUBLISHED;

  // Set the author of the node.
  $node->uid = $GLOBALS['user']->uid ?: 1;

  // Map webform fields to node fields.
  $node->field_first_name['und'][0]['value'] = check_plain($data['first_name']);
  $node->field_last_name['und'][0]['value'] = check_plain($data['last_name']);
  $node->body['und'][0]['value'] = filter_xss($data['bio']);  
  $node->title = trim($data['first_name'] . ' ' . $data['last_name'] . ' Biography');

  // Save the node.
  $node->save();

  // Process the photo field.
  if (!empty($data['photo'])) {
    $file = file_load($data['photo']);
    if ($file) {
      $node->field_image['und'][0] = [
        'fid' => $file->fid,
        'alt' => 'Photo of ' . $data['first_name'] . ' ' . $data['last_name'],
      ];

      // Save the node again with the image.
      $node->save();

      // Associate the file with the node.
      file_usage_add($file, 'node', 'node', $node->nid);
    }
  }

  watchdog('webform_to_node', 'Created node @nid from webform submission.', [
    '@nid' => $node->nid,
  ]);
}
willowf's picture

Great, thanks a lot I will try it, there should be a webform module that covers this. Regards