You are here

class TermDuplicate in Commerce Bulk 8

Duplicate term.

Plugin annotation


@Action(
  id = "commerce_bulk_term_duplicate",
  label = @Translation("Duplicate term"),
  type = "taxonomy_term"
)

Hierarchy

Expanded class hierarchy of TermDuplicate

File

src/Plugin/Action/TermDuplicate.php, line 21

Namespace

Drupal\commerce_bulk\Plugin\Action
View 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

Namesort descending Modifiers Type Description Overrides
ConfigurableActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
ConfigurableActionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConfigurableActionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ConfigurableActionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
ConfigurableActionBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 6
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TermDuplicate::access public function Checks object access. Overrides ActionInterface::access
TermDuplicate::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
TermDuplicate::dashTerms public static function
TermDuplicate::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableActionBase::defaultConfiguration
TermDuplicate::execute public function Executes the plugin. Overrides ExecutableInterface::execute
TermDuplicate::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionBase::executeMultiple
TermDuplicate::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm