You are here

function nodeorder_taxonomy_vocabulary_form_submit in Node Order 8

Submit handler for nodeorder_form_taxonomy_vocabulary_form_alter().

Related topics

1 string reference to 'nodeorder_taxonomy_vocabulary_form_submit'
nodeorder_form_taxonomy_vocabulary_form_alter in ./nodeorder.module
Implements hook_form_FORM_ID_alter() for taxonomy_form_vocabulary().

File

./nodeorder.module, line 88
Nodeorder module.

Code

function nodeorder_taxonomy_vocabulary_form_submit($form, FormStateInterface $form_state) {

  /** @var \Drupal\nodeorder\NodeOrderManagerInterface $nodeorder_manager */
  $nodeorder_manager = \Drupal::service('nodeorder.manager');
  $vocabulary = $form_state
    ->getFormObject()
    ->getEntity();
  $config = \Drupal::configFactory()
    ->getEditable('nodeorder.settings');
  $orderable = $form_state
    ->getValue('orderable');
  $vocabularies = $config
    ->get('vocabularies');
  if ($orderable && empty($vocabularies[$vocabulary
    ->id()])) {

    // Switching from non-orderable to orderable.
    Cache::invalidateTags([
      'nodeorder',
    ]);

    // Set weight to nid for all rows in term_node where the tid is in this
    // vocabulary.
    $tree = \Drupal::service('entity_type.manager')
      ->getStorage("taxonomy_term")
      ->loadTree($vocabulary
      ->id());
    $tids = [];
    foreach ($tree as $term) {
      $tids[] = $term->tid;
    }
    if (count($tids) > 0) {
      $order = 'n.sticky DESC, tn0.weight';
      $tid = 0;
      $query_max = \Drupal::database()
        ->select('taxonomy_index', 'ti')
        ->condition('tid', $tid);
      $query_max
        ->addExpression('MAX(weight)', 'mweight');
      foreach ($tids as $tid) {

        // Select *current* nodes for the current term.
        // @todo: Replace this hacked function call.
        $result = $nodeorder_manager
          ->selectNodes([
          $tid,
        ], 'and', 0, FALSE, $order, 0);
        foreach ($result as $node) {
          $max = $query_max
            ->execute()
            ->fetchField();
          $max = (int) $max + 1;
          \Drupal::database()
            ->update('taxonomy_index')
            ->condition('nid', $node->nid)
            ->condition('tid', $tid)
            ->fields([
            'weight' => $max,
          ])
            ->execute();
        }
      }
    }
    \Drupal::messenger()
      ->addStatus(t('You may now order nodes within this vocabulary.'));
  }
  elseif (!$orderable && !empty($vocabularies[$vocabulary
    ->id()])) {

    // Switching from orderable to non-orderable.
    Cache::invalidateTags([
      'nodeorder',
    ]);

    // Set weight to 0 for all rows in term_node where the tid is in this
    // vocabulary.
    $tree = \Drupal::service('entity_type.manager')
      ->getStorage("taxonomy_term")
      ->loadTree($vocabulary
      ->id());
    $tids = [];
    foreach ($tree as $term) {
      $tids[] = $term->tid;
    }
    if (count($tids) > 0) {
      \Drupal::database()
        ->update('taxonomy_index')
        ->fields([
        'weight' => 0,
      ])
        ->condition('tid', $tids, 'IN')
        ->execute();
    }
    \Drupal::messenger()
      ->addStatus(t('You may no longer order nodes within this vocabulary.'));
  }

  // Update config.
  $vocabularies[$vocabulary
    ->id()] = $orderable;
  $config
    ->set('vocabularies', $vocabularies);
  $config
    ->save();
}