class TermDuplicate in Commerce Bulk 8
Duplicate term.
Plugin annotation
@Action(
id = "commerce_bulk_term_duplicate",
label = @Translation("Duplicate term"),
type = "taxonomy_term"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Action\ConfigurableActionBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, PluginFormInterface
- class \Drupal\commerce_bulk\Plugin\Action\TermDuplicate
- class \Drupal\Core\Action\ConfigurableActionBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, PluginFormInterface
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TermDuplicate
File
- src/
Plugin/ Action/ TermDuplicate.php, line 21
Namespace
Drupal\commerce_bulk\Plugin\ActionView source
class TermDuplicate extends ConfigurableActionBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$messenger = \Drupal::messenger();
$request = \Drupal::request();
$storage = \Drupal::service('entity_type.manager')
->getStorage('taxonomy_term');
if ($ids = explode('|', $request->query
->get('ids'))) {
$tree = $storage
->loadTree($storage
->load(end($ids))
->bundle(), 0, NULL, TRUE);
$names = '';
$terms = $name_ids = [];
// $dashed = $this->dashTerms('ru-RU', '','',1); || $dashed[$id] != $name
foreach ($tree as $term) {
$id = $term
->id();
$name = $term
->getName();
if (!in_array($id, $ids)) {
$messenger
->addError("The term {$id}={$name} is wrong!!!!");
}
$name = "{$id}=" . str_repeat('-', $term->depth) . $name . PHP_EOL;
$names .= $name;
$terms[$id] = $term;
}
$values = [];
$form_state
->set('term', end($values));
$form_state
->set('terms', $terms);
$readmehelp = readmehelp_converter_service();
$path = $readmehelp->moduleHandler
->getModuleDirectories()['commerce_bulk'] . '/commerce_bulk.module';
$form['warning'] = [
'#markup' => new TranslatableMarkup('<h2>Change and / or add terms for %count <span style="color:red">Name</span>s. New <em>Names</em> should be inserted after the last existing: one <em>Name</em> on each line with prepended with one dash "-" symbol for a child term, two dashes for a grandchild and so on.', [
'%count' => count($terms),
]),
];
$form['names'] = [
'#type' => 'textarea',
'#title' => $this
->t('Names'),
'#default_value' => $names,
'#rows' => 20,
];
$form['data_warning'] = [
'#markup' => new TranslatableMarkup('<h3><mark>Tip:</mark> Optionally, you can pass <em>XML</em> or <em>JSON</em> data and alter each term value in the <mark>YOUR_MODULE_commerce_bulk_term_new_alter()</mark>. See example hook implementation in the commerce_bulk.module file:</h3><div style="border:1px solid grey">' . $readmehelp
->highlightPhp($path, 74, 8) . '</div>'),
];
$form['data'] = [
'#type' => 'textarea',
'#title' => $this
->t('Optional JSON or XML data'),
'#rows' => 1,
];
$form['cancel'] = [
'#type' => 'submit',
'#value' => 'CANCEL AND BACK',
'#weight' => 1000,
];
// Remove the "Action was applied to N items" message.
$messenger
->deleteByType('status');
}
return $form;
}
/**
* {@inheritdoc}
*/
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;
}
}
}
}
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $terms) {
if ($terms) {
$ids = [];
foreach ($terms as $term) {
$ids[] = $term
->id();
}
$query = [
'destination' => \Drupal::request()
->getRequestUri(),
'ids' => implode('|', $ids),
];
$path = Url::fromUserInput('/admin/config/system/actions/configure/' . $this
->getPluginId(), [
'query' => $query,
])
->toString();
$response = new RedirectResponse($path);
$response
->send();
}
}
/**
* {@inheritdoc}
*/
public function execute($term = NULL) {
// Do nothing.
}
/**
* {@inheritdoc}
*/
public function access($term, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = $term
->access('create', $account, TRUE);
return $return_as_object ? $result : $result
->isAllowed();
}
/**
* {@inheritdoc}
*/
public static function dashTerms($file = '', $match = '', $engfile = '', $array = NULL) {
$files = \Drupal::service('file_system');
$file = $files
->realpath("private://google/taxonomy-with-ids.{$file}.txt");
if (file_exists($file)) {
$file = file_get_contents($file);
}
if ($match) {
$num = preg_match_all('/^\\d+\\ -\\ ' . $match . '.*/m', $file, $matches);
$file = $matches[0];
}
$eng = $files
->realpath("private://google/taxonomy-with-ids.{$engfile}.txt");
if ($engfile = file_exists($eng)) {
$engfile = file_get_contents($eng);
$engfile = explode(PHP_EOL, trim($engfile));
$eng = [];
foreach ($engfile as $line) {
$line = explode(' - ', $line);
$eng[$line[0]] = $line[1];
}
}
$file = is_array($file) ? $file : explode(PHP_EOL, trim($file));
$terms = $keyed = [];
foreach ($file as $index => $line) {
$parts = explode(' - ', $line);
$dash = '';
$names = explode(' > ', $parts[1]);
foreach ($names as $term) {
if (!isset($terms["{$dash}{$term}"])) {
$engpart = $engfile ? "={$eng[$parts[0]]}" : '';
$terms["{$dash}{$term}"] = "={$parts[0]}{$engpart}";
$keyed[$parts[0]] = $term;
}
$dash .= '-';
}
}
$file = '';
foreach ($terms as $key => $value) {
$file .= "={$key}{$value}" . PHP_EOL;
}
return $array ? $keyed : $file;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurableActionBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
ConfigurableActionBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ConfigurableActionBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ConfigurableActionBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
ConfigurableActionBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
6 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TermDuplicate:: |
public | function |
Checks object access. Overrides ActionInterface:: |
|
TermDuplicate:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
TermDuplicate:: |
public static | function | ||
TermDuplicate:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableActionBase:: |
|
TermDuplicate:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
TermDuplicate:: |
public | function |
Executes the plugin for an array of objects. Overrides ActionBase:: |
|
TermDuplicate:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |