public function TermDuplicate::submitConfigurationForm in Commerce Bulk 8
Form submission handler.
Parameters
array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Overrides PluginFormInterface::submitConfigurationForm
File
- src/
Plugin/ Action/ TermDuplicate.php, line 89
Class
- TermDuplicate
- Duplicate term.
Namespace
Drupal\commerce_bulk\Plugin\ActionCode
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#id'] != 'edit-cancel') {
$names = explode(PHP_EOL, trim($form_state
->getValue('names')));
$names = array_filter($names, function ($name) {
return trim($name);
});
if ($names) {
$module_handler = \Drupal::moduleHandler();
$data = $form_state
->get('data');
$terms = $form_state
->get('terms');
$form_state
->set('terms', NULL);
$parents = [];
$root = $prev = reset($terms);
$parents[$root->depth] = $root
->id();
$weight = $root
->getWeight();
$timestamp = time();
foreach ($names as $index => $name) {
$parts = explode('=', $name);
$tid = !isset($parts[2]) && isset($parts[1]) ? $parts[0] : (isset($parts[2]) ? trim($parts[2]) : 0);
$name = isset($parts[1]) ? $parts[1] : $parts[0];
if (($name = trim($name)) && ($len = strlen($name)) && ($name = ltrim($name, '-'))) {
$parents = array_filter($parents);
$parent = end($parents);
if (isset($terms[$tid])) {
$term = $terms[$tid];
$weight = $term
->getWeight();
}
else {
$term = $prev
->createDuplicate();
if ($tid) {
$term
->set('tid', $tid);
}
$weight = !empty($terms[$parent]) ? $terms[$parent]
->getWeight() : $weight;
$term
->setWeight(++$weight);
}
$depth = $len - strlen($name);
$last_depth = array_flip($parents);
$last_depth = end($last_depth);
if (!$root) {
$depth = $depth < 1 ? 1 : $depth;
$depth = $depth - $last_depth > 1 ? $last_depth + 1 : $depth;
if ($depth > $last_depth) {
$term
->set('parent', $parents[$last_depth]);
}
elseif ($depth == $last_depth && !isset($parents[$depth - 1])) {
$term
->set('parent', $prev
->id());
}
else {
$term
->set('parent', $parents[$depth - 1]);
}
}
$term
->setChangedTime($timestamp);
$timestamp--;
$module_handler
->alter('commerce_bulk_term_new', $term, $name, $data);
$term
->setName($name)
->save();
if (!$root) {
$parents[$depth] = $term
->id();
}
$root = FALSE;
$prev = $term;
}
}
}
}
}