entity_machine_name-1.0.0-beta3/entity_machine_name.install

entity_machine_name.install
<?php

/**
 * @file
 * Install, update functions for the Taxonomy Machine Name module.
 */

use Drupal\Core\Database\Database;
use Drupal\taxonomy\Entity\Term;
use Drupal\node\Entity\Node;

/**
 * Implements hook_install().
 */
function entity_machine_name_install() {
  batch_set(
    [
      'file' => drupal_get_path('module', 'entity_machine_name') . '/entity_machine_name.install',
      'title' => t('Generating missing taxonomy term machine names'),
      'init_message' => t('Starting taxonomy term machine names update'),
      'error_message' => t('Error updating taxonomy term machine names'),
      'operations' => [
        ['entity_machine_name_update_terms', []],
        ['entity_machine_name_update_nodes', []],
      ],
    ]
  );
}

/**
 * Update machine names for existing terms, usable both in batch and update.
 *
 * @param array $context
 *   The $context parameter in updates, called $context in Batch API.
 */
function entity_machine_name_update_terms(&$context) {
  $sandbox = &$context['sandbox'];
  if (empty($sandbox['tids'])) {
    // Size of the batch to process.
    $batch_size = 10;

    $tids = \Drupal::entityQuery('taxonomy_term')->notExists('machine_name')->execute();

    $sandbox['total'] = count($tids);
    $sandbox['tids'] = array_chunk($tids, $batch_size);
    $sandbox['succeeded'] = $sandbox['errored'] = $sandbox['processed_chunks'] = 0;
  }

  // Nothing to do.
  if (!$sandbox['total']) {
    $context['message'] = t('No terms updated');
    return;
  }

  // Process all terms in this chunk.
  $current_chunk = $sandbox['tids'][$sandbox['processed_chunks']];
  $terms = Term::loadMultiple($current_chunk);

  foreach ($terms as $term) {
    $success = entity_machine_name_update($term);
    $success ? $sandbox['succeeded']++ : $sandbox['errored']++;
  }

  // Increment the number of processed chunks to determine when we've finished.
  $sandbox['processed_chunks']++;

  // When we have processed all of the chunks $context['finished'] will be 1.
  // Then the batch / update runner will consider the job finished.
  $context['finished'] = $sandbox['processed_chunks'] / count($sandbox['tids']);

  $context['message'] = t(
    '@succeeded terms were updated correctly. @errored terms failed.',
    [
      '@succeeded' => $sandbox['succeeded'],
      '@errored' => $sandbox['errored'],
    ]
  );
}

/**
 * Update machine names for existing terms, usable both in batch and update.
 *
 * @param array $context
 *   The $context parameter in updates, called $context in Batch API.
 */
function entity_machine_name_update_nodes(&$context) {
  $sandbox = &$context['sandbox'];
  if (empty($sandbox['ids'])) {
    // Size of the batch to process.
    $batch_size = 10;

    $entity_ids = \Drupal::entityQuery('node')->notExists('machine_name')->execute();

    $sandbox['total'] = count($entity_ids);
    $sandbox['ids'] = array_chunk($entity_ids, $batch_size);
    $sandbox['succeeded'] = $sandbox['errored'] = $sandbox['processed_chunks'] = 0;
  }

  // Nothing to do.
  if (!$sandbox['total']) {
    $context['message'] = t('No entities updated');
    return;
  }

  // Process all terms in this chunk.
  $current_chunk = $sandbox['ids'][$sandbox['processed_chunks']];
  $entities = Node::loadMultiple($current_chunk);

  foreach ($entities as $entity) {
    $success = entity_machine_name_entities_update($entity);
    $success ? $sandbox['succeeded']++ : $sandbox['errored']++;
  }

  // Increment the number of processed chunks to determine when we've finished.
  $sandbox['processed_chunks']++;

  // When we have processed all of the chunks $context['finished'] will be 1.
  // Then the batch / update runner will consider the job finished.
  $context['finished'] = $sandbox['processed_chunks'] / count($sandbox['ids']);

  $context['message'] = t(
    '@succeeded entities were updated correctly. @errored entities failed.',
    [
      '@succeeded' => $sandbox['succeeded'],
      '@errored' => $sandbox['errored'],
    ]
  );
}

/**
 * Implements hook_uninstall().
 */
function entity_machine_name_uninstall() {
  $db_connection = Database::getConnection();
  $db_connection->update('taxonomy_term_field_data')
    ->fields(['machine_name' => NULL])
    ->execute();
  $db_connection = Database::getConnection();
  $db_connection->update('node_field_data')
    ->fields(['machine_name' => NULL])
    ->execute();
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc