Passing form value into batch process I have a form that is used to trigger a bulk operation and add a node for each of the currently registered users on the site. This is all working well, but I'd like to pass form data from BulkAddForm.php to the batch operation in BulkAddNode.php

If is were to setup a variable called $year in BulkAddForm which contained form state. How would I then use it in a line like this...

'title' =&gt; 'Perf-plan-' . $year . '-' . $user-&gt;get('field_user_employeeid')-&gt;value,


BulkAddForm.php

&lt;?php
 namespace Drupal\create_perf_plan_for_new_year\Form;
 use Drupal\Core\Form\FormBase;
 use Drupal\Core\Form\FormStateInterface;
/**
 * Class BulkAddForm.
 *
 * @package Drupal\create_perf_plan_for_new_year\Form
 */
class BulkAddForm extends FormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'bulk_add_form';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['perfPlanYear'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; 'Year Tag (1819 for instance)',
    );
    $form['bulk_add'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; $this-&gt;t('Create new Performance Plans'),
    );
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $uids = \Drupal::entityQuery('user')
      //-&gt;condition('type', 'batch')
      -&gt;sort('created', 'ASC')
      -&gt;execute();
    $batch = array(
      'title' =&gt; t('Bulk Add happening within the Bulk Add Form file...'),
      'operations' =&gt; array(
        array(
          '\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAdd',
          array($uids)
        ),
      ),
      'finished' =&gt; '\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAddFinishedCallback',
    );
    batch_set($batch);
  }
}


BulkAddNode.php

&lt;?php
namespace Drupal\create_perf_plan_for_new_year;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\Core\Url;

class BulkAddNode {

    public static function bulkAdd($uids, &amp;$context){
    $message = 'Bulk add happening in functional file...';
    foreach ($uids as $uid) {
        $user = \Drupal\user\Entity\User::load($uid);
        \Drupal\node\Entity\Node::create([
            'type' =&gt; 'perf_plan',
            'title' =&gt; 'Perf-plan-' . '1718' . '-' . $user-&gt;get('field_user_employeeid')-&gt;value,
            'uid' =&gt; $user-&gt;id(),
            'field_employee_uid' =&gt; $user-&gt;id(),
            //'field_supervisor_uid' =&gt; $user-&gt;get('field_supervisor')-&gt;first()-&gt;getValue()['target_id'], //Brilliant means of getting entity reference UID from User. SM
            //'field_director_uid' =&gt; $user-&gt;get('field_director')-&gt;first()-&gt;getValue()['target_id'],
          ])-&gt;save();
    }
    $context['message'] = $message;
  }

function bulkAddFinishedCallback($success, $operations) {
    if ($success) {
      $message = 'Performance plan added.';
    }
    else {
      $message = t('Encountered an error during bulk performance plan creation.');
    }
    drupal_set_message($message);
  }
}
You can pass variables to the batch process by doing the following...

     $year = "2018";
     $batch = array(
          'title' =&gt; t('Bulk Add happening within the Bulk Add Form file...'),
          'operations' =&gt; array(
            array(
              '\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAdd',
              array($uids, $year)
            ),
          ),
          'finished' =&gt; '\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAddFinishedCallback',
        );


Year in this case can obviously be anything... a piece of $form_state for instance also works.

You could then change 

public static function bulkAdd($uids, $year, &amp;$context){
    $message = 'Bulk add happening in functional file...';
    foreach ($uids as $uid) {
        $user = \Drupal\user\Entity\User::load($uid);
        \Drupal\node\Entity\Node::create([
            'type' =&gt; 'perf_plan',
            'title' =&gt; 'Perf-plan-' . $year . '-' . $user-&gt;get('field_user_employeeid')-&gt;value,
            'uid' =&gt; $user-&gt;id(),
            'field_employee_uid' =&gt; $user-&gt;id(),
            //'field_supervisor_uid' =&gt; $user-&gt;get('field_supervisor')-&gt;first()-&gt;getValue()['target_id'], //Brilliant means of getting entity reference UID from User. SM
            //'field_director_uid' =&gt; $user-&gt;get('field_director')-&gt;first()-&gt;getValue()['target_id'],
          ])-&gt;save();
    }
    $context['message'] = $message;
  }


Hope this helps. It took me a while to figure out where in the batch array structure this needed to be placed. I didn't find the docs particularly helpful.