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' => 'Perf-plan-' . $year . '-' . $user->get('field_user_employeeid')->value,
BulkAddForm.php
<?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' => 'textfield',
'#title' => 'Year Tag (1819 for instance)',
);
$form['bulk_add'] = array(
'#type' => 'submit',
'#value' => $this->t('Create new Performance Plans'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$uids = \Drupal::entityQuery('user')
//->condition('type', 'batch')
->sort('created', 'ASC')
->execute();
$batch = array(
'title' => t('Bulk Add happening within the Bulk Add Form file...'),
'operations' => array(
array(
'\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAdd',
array($uids)
),
),
'finished' => '\Drupal\create_perf_plan_for_new_year\BulkAddNode::bulkAddFinishedCallback',
);
batch_set($batch);
}
}
BulkAddNode.php
<?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, &$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' => 'perf_plan',
'title' => 'Perf-plan-' . '1718' . '-' . $user->get('field_user_employeeid')->value,
'uid' => $user->id(),
'field_employee_uid' => $user->id(),
//'field_supervisor_uid' => $user->get('field_supervisor')->first()->getValue()['target_id'], //Brilliant means of getting entity reference UID from User. SM
//'field_director_uid' => $user->get('field_director')->first()->getValue()['target_id'],
])->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);
}
}