You are here

_old.gc.module in GatherContent 8

File

_old.gc.module
View source
<?php

///**

// * @FIXME
// * This implementation of hook_menu() cannot be automatically converted because
// * it contains logic (i.e., branching statements, function calls, object
// * instantiation, etc.) You will need to convert it manually. Sorry!
// *
// * For more information on how to convert hook_menu() to Drupal 8's new routing
// * and linking systems, see https://api.drupal.org/api/drupal/core%21includes%21menu.inc/group/menu/8
// */

//function gc_menu() {

//  $items = array();
//  $items['admin/config/gc'] = array(
//    'title' => 'GatherContent',
//    'page callback' => 'system_admin_menu_block_page',
//    'access arguments' => array('administer gathercontent'),
//    'file' => 'system.admin.inc',
//    'file path' => drupal_get_path('module', 'system'),
//  );
//  $items['admin/config/gc/config'] = array(
//    'title' => 'Authentication',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('gc_authentication_form'),
//    'access arguments' => array('administer gathercontent'),
//    'file' => 'gc.authentication.inc',
//  );
//  $items['admin/config/gc/config/config'] = array(
//    'title' => 'Authentication',
//    'access arguments' => array('administer gathercontent'),
//    'type' => MENU_DEFAULT_LOCAL_TASK,
//  );
//  $items['admin/config/gc/config/test'] = array(
//    'title' => 'Test connection',
//    'page callback' => 'gc_authentication_test_page',
//    'access arguments' => array('administer gathercontent'),
//    'file' => 'gc.authentication.inc',
//    'type' => MENU_LOCAL_TASK,
//  );
//  $items['admin/config/gc/mapping/create'] = array(
//    'title' => 'Add GatherContent Template',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('gc_mapping_form_templates'),
//    'access arguments' => array(
//      'administer gathercontent',
//    ),
//    'file' => 'forms/gc.mapping-create.inc',
//  );
//  $items['admin/config/gc/mapping/%/delete'] = array(
//    'title' => 'Delete GatherContent Mapping',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('gc_mapping_delete_form', 4),
//    'access arguments' => array(
//      'administer gathercontent',
//    ),
//    'type' => MENU_CALLBACK,
//    'file' => 'forms/gc.mapping-delete.inc',
//  );
//  $items['admin/config/gc/mapping/%/edit'] = array(
//    'title' => 'Edit GatherContent Mapping',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('gc_mapping_edit_form', 4),
//    'access arguments' => array(
//      'administer gathercontent',
//    ),
//    'type' => MENU_CALLBACK,
//    'file' => 'forms/gc.mapping-edit.inc',
//  );
//  $items['admin/config/gc/import'] = array(
//    'title' => 'Import Content',
//    'page callback' => 'gc_import_form',
//    'access arguments' => array(
//      'administer gathercontent',
//    ),
//    'file' => 'gc.import.inc',
//  );
//  return $items;

//}

//

///**

// * Implements hook_menu_local_tasks_alter().
// */

//function gc_menu_local_tasks_alter(&$data, $router_item, $root_path) {

//  if ($root_path === 'admin/config/gc/mapping') {
//    $data['actions']['output'][] = array(
//      '#theme' => 'menu_local_task',
//      '#link' => array(
//        'title' => t('Add new GatherContent template'),
//        'href' => 'admin/config/gc/mapping/create',
//        'localized_options' => array(
//          'attributes' => array(
//            'title' => t('Add new GatherContent template'),
//          ),
//        ),
//      ),
//    );
//  }

//}

///**

// * Implements hook_entity_info().
// */

//function gc_entity_info() {

//  $info = array();
//
//  $info['gc_mapping'] = array(
//    'label' => t('Mapping'),
//    'base table' => 'gc_mapping',
//    'entity keys' => array(
//      'id' => 'id',
//      'label' => 'content_type',
//    ),
//    'module' => 'gc',
//    'entity class' => 'Entity',
//    'controller class' => 'EntityAPIController',
//    'views controller class' => 'EntityDefaultViewsController',
//    'fieldable' => FALSE,
//  );
//
//  $info['gc_operation'] = array(
//    'label' => t('GC Operation'),
//    'base table' => 'gc_operation',
//    'entity keys' => array(
//      'id' => 'uuid',
//      'label' => 'uuid',
//    ),
//    'module' => 'gc',
//    'entity class' => 'Entity',
//    'controller class' => 'EntityAPIController',
//    'views controller class' => 'EntityDefaultViewsController',
//    'fieldable' => FALSE,
//  );
//
//  $info['gc_operation_item'] = array(
//    'label' => t('GC Operation Item'),
//    'base table' => 'gc_operation_item',
//    'entity keys' => array(
//      'id' => 'id',
//    ),
//    'module' => 'gc',
//    'entity class' => 'Entity',
//    'controller class' => 'EntityAPIController',
//    'views controller class' => 'EntityDefaultViewsController',
//    'fieldable' => FALSE,
//  );
//
//  return $info;

//}

//

///**

// * Implements hook_entity_property_info().
// */

//function gc_entity_property_info() {

//  $info = array();
//
//  $info['gc_mapping']['properties']['id'] = array(
//    'label' => t('Mapping ID'),
//    'description' => t('The ID of the mapping.'),
//    'type' => 'integer',
//    'schema field' => 'id',
//  );
//  $info['gc_mapping']['properties']['gc_project_id'] = array(
//    'label' => t('GC Project ID'),
//    'description' => t('The ID of the project in GatherContent.'),
//    'type' => 'integer',
//    'schema field' => 'gc_project_id',
//  );
//  $info['gc_mapping']['properties']['gc_project'] = array(
//    'label' => t('GC Project'),
//    'description' => t('Project name in GatherContent.'),
//    'type' => 'text',
//    'schema field' => 'gc_project',
//  );
//  $info['gc_mapping']['properties']['gc_template_id'] = array(
//    'label' => t('GC Template ID'),
//    'description' => t('The ID of template in GatherContent.'),
//    'type' => 'integer',
//    'schema field' => 'gc_template_id',
//  );
//  $info['gc_mapping']['properties']['gc_template'] = array(
//    'label' => t('GC Template'),
//    'description' => t('Name of template in GatherContent.'),
//    'type' => 'text',
//    'schema field' => 'gc_template',
//  );
//  $info['gc_mapping']['properties']['content_type'] = array(
//    'label' => t('Content type machine name'),
//    'description' => t('Content type machine name.'),
//    'type' => 'token',
//    'schema field' => 'content_type',
//  );
//  $info['gc_mapping']['properties']['content_type_name'] = array(
//    'label' => t('Content type name'),
//    'description' => t('Content type human-readable name.'),
//    'type' => 'text',
//    'computed' => TRUE,
//    'getter callback' => 'entity_property_verbatim_set',
//    'options list' => 'node_type_get_names',
//    'schema field' => 'content_type',
//  );
//  $info['gc_mapping']['properties']['created'] = array(
//    'label' => t('Created'),
//    'description' => t('Timestamp of mapping creation'),
//    'type' => 'date',
//    'getter callback' => 'entity_property_verbatim_set',
//    'schema field' => 'created',
//  );
//  $info['gc_mapping']['properties']['updated_drupal'] = array(
//    'label' => t('Updated (Drupal)'),
//    'description' => t('Timestamp of mapping update in Drupal'),
//    'type' => 'date',
//    'schema field' => 'updated_drupal',
//  );
//  $info['node']['properties']['gc_mapping_id'] = array(
//    'label' => t("GC Mapping ID"),
//    'type' => 'integer',
//    'description' => t("The ID of created GatherContent mapping"),
//  );
//  $info['node']['properties']['gc_id'] = array(
//    'label' => t("GC ID"),
//    'type' => 'integer',
//    'description' => t("The ID of content in GatherContent"),
//  );
//  $info['node']['properties']['gc_import_status'] = array(
//    'label' => t("GC Import status"),
//    'type' => 'text',
//    'description' => t("Status of import from GatherContent"),
//  );
//  $info['file']['properties']['gc_id'] = array(
//    'label' => t("GC ID"),
//    'type' => 'integer',
//    'description' => t("The ID of file in GatherContent"),
//  );
//  $info['gc_operation']['properties']['uuid'] = array(
//    'label' => t('Operation UUID'),
//    'type' => 'text',
//    'schema field' => 'uuid',
//  );
//  $info['gc_operation']['properties']['type'] = array(
//    'label' => t('Operation Type'),
//    'type' => 'text',
//    'schema field' => 'type',
//  );
//  $info['gc_operation_item']['properties']['id'] = array(
//    'label' => t('Operation Item ID'),
//    'type' => 'integer',
//    'schema field' => 'id',
//  );
//  $info['gc_operation_item']['properties']['operation_uuid'] = array(
//    'label' => t('Operation UUID'),
//    'type' => 'text',
//    'schema field' => 'operation_uuid',
//  );
//  $info['gc_operation_item']['properties']['item_status'] = array(
//    'label' => t('Operation Item Status'),
//    'type' => 'text',
//    'schema field' => 'item_status',
//  );
//  $info['gc_operation_item']['properties']['item_status_color'] = array(
//    'label' => t('Operation Item Status Color'),
//    'type' => 'text',
//    'schema field' => 'item_status_color',
//  );
//  $info['gc_operation_item']['properties']['item_name'] = array(
//    'label' => t('Operation Item Name'),
//    'type' => 'text',
//    'schema field' => 'item_name',
//  );
//  $info['gc_operation_item']['properties']['template_name'] = array(
//    'label' => t('Operation Item Template Name'),
//    'type' => 'text',
//    'schema field' => 'template_name',
//  );
//  $info['gc_operation_item']['properties']['status'] = array(
//    'label' => t('Operation Item Operation Status'),
//    'type' => 'text',
//    'schema field' => 'status',
//  );
//  $info['gc_operation_item']['properties']['gc_id'] = array(
//    'label' => t('Operation Item GC ID'),
//    'type' => 'integer',
//    'schema field' => 'gc_id',
//  );
//  $info['gc_operation_item']['properties']['nid'] = array(
//    'label' => t('Operation Item NID'),
//    'type' => 'integer',
//    'schema field' => 'nid',
//  );
//
//  return $info;

//}

//

///**

// * Implements hook_views_api().
// */

//function gc_views_api() {

//  return array(
//    'api' => 3,
//    'path' => drupal_get_path('module', 'gc') . '/views',
//  );

//}

//

///**

// * Implements hook_action_info().
// *
// * @inheritdoc
// */

//function gc_action_info() {

//  return array(
//    'gc_update' => array(
//      'type' => 'entity',
//      'label' => t('Update from GatherContent'),
//      'configurable' => FALSE,
//      'triggers' => array('any'),
//      'behavior' => array('create_property'),
//      'vbo_configurable' => FALSE,
//      'pass rows' => TRUE,
//    ),
//    'gc_upload' => array(
//      'type' => 'entity',
//      'label' => t('Upload to GatherContent'),
//      'configurable' => FALSE,
//      'triggers' => array('any'),
//      'behavior' => array('create_property'),
//      'vbo_configurable' => FALSE,
//      'pass rows' => TRUE,
//    ),
//  );

//}

/**
* Implements hook_views_pre_render().
*
* Show real-time data in views resources "effective" way.
*/
function gc_views_pre_render(&$view) {
  if ($view->name == 'update') {
    $results =& $view->result;
    $used_mappings = array();
    $nids = array();
    foreach ($results as $result) {
      $nids[] = $result->nid;
    }
    $nodes = \Drupal::entityManager()
      ->getStorage('node')
      ->loadMultiple($nids);
    foreach ($nodes as $node) {
      if (!in_array($node->gc_mapping_id, $used_mappings)) {
        $used_mappings[] = $node->gc_mapping_id;
      }
    }
    $selected_projects = array();
    $contents = array();
    $content_obj = new Content();
    $mappings = \Drupal::entityManager()
      ->getStorage('gc_mapping');
    foreach ($mappings as $mapping) {
      if (!in_array($mapping->gc_project_id, $selected_projects)) {
        $selected_projects[] = $mapping->gc_project_id;
        $content = $content_obj
          ->getContents($mapping->gc_project_id);
        foreach ($content as $c) {
          $single_content = array();
          $single_content['gc_updated'] = $c->updated_at;
          $single_content['status'] = $c->status;
          $single_content['name'] = $c->name;
          $contents[$c->id] = $single_content;
        }
      }
    }
    $view->field['gc_status']->contents = $contents;
  }
  if ($view->name == 'mapping') {
    $project_obj = new Project();
    $projects = $project_obj
      ->getProjectObjects();
    $temp_obj = new Template();
    $templates = array();
    foreach ($projects as $project) {
      $remote_templates = $temp_obj
        ->getTemplatesObject($project->id);
      foreach ($remote_templates as $remote_template) {
        $templates[$remote_template->id]['updated_at'] = $remote_template->updated_at;
      }
    }
    $view->field['gc_updated']->templates = $templates;
  }
}

/**
* Implements hook_views_bulk_operations_form_alter().
*
* Alter VBO confirm form for gc_update action and replace submit callback,
* so we can use custom batch.
*/
function gc_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
  if ($form_state['step'] === 'views_bulk_operations_confirm_form') {
    switch ($form_state['values']['operation']) {
      case 'action::gc_update':
        $array = array(
          implode('+', array_filter($form_state['values']['views_bulk_operations'])),
        );
        $view = views_get_view('update');
        $view
          ->set_display("block_1");
        $view
          ->set_arguments($array);
        $view
          ->preview();
        $content = $view
          ->render();
        $form['description']['#markup'] = '<p>' . t('Please review your selection before updating.') . '</p>' . $content;
        $form['#vbo_confirm_form_title'] = \Drupal::translation()
          ->formatPlural(count(array_filter($form_state['values']['views_bulk_operations'])), 'Confirm update selection (@count item)', 'Confirm update selection (@count items)');
        $form['actions']['submit']['#submit'] = array(
          'gc_views_vbo_update',
        );
        break;
      case 'action::gc_upload':
        $array = array(
          implode('+', array_filter($form_state['values']['views_bulk_operations'])),
        );
        $view = views_get_view('update');
        $view
          ->set_display("block_1");
        $view
          ->set_arguments($array);
        $view
          ->preview();
        $content = $view
          ->render();
        $form['description']['#markup'] = '<p>' . t('Please review your selection before upload.') . '</p>' . $content;
        $form['#vbo_confirm_form_title'] = \Drupal::translation()
          ->formatPlural(count(array_filter($form_state['values']['views_bulk_operations'])), 'Confirm upload selection (@count item)', 'Confirm upload selection (@count items)');
        $form['actions']['submit']['#submit'] = array(
          'gc_views_vbo_upload',
        );
        break;
    }
  }
}

/**
* Submit callback for `views_bulk_operations_confirm_form`.
*
* This callback is used for `gc_update` action and it is substitution for VBO
* in order to redirect to custom view after operation is completed.
*
* @inheritdoc
*/
function gc_views_vbo_update($form, &$form_state) {
  $uuid = _gc_uuid_generate();
  $operation = \Drupal::entityManager()
    ->getStorage('gc_operation')
    ->create(array(
    'uuid' => $uuid,
    'type' => 'update',
  ));
  $operation
    ->save();
  $operations = array();
  $nodes = \Drupal::entityManager()
    ->getStorage('node')
    ->loadMultiple($form_state['selection']);
  foreach ($nodes as $node) {
    $operations[] = array(
      'gc_update_process',
      array(
        $node,
        $uuid,
      ),
    );
  }
  $batch = array(
    'title' => t('Updating content ...'),
    'operations' => $operations,
    'finished' => 'gc_update_finished',
    'init_message' => t('Update is starting ...'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('An error occurred during processing'),
  );
  batch_set($batch);
}

/**
* Batch operation callback.
*
* We are doing real update thing here.
*
* @param object $node
*   Object of content we want to update.
*/
function gc_update_process($node, $uuid, &$context) {
  _gc_fetcher($node->gc_id, $uuid, NULL, $node);
  $context['results']['uuid'] = $uuid;
}

/**
* Finished callback.
*
* @inheritdoc
*/
function gc_update_finished($success, $results, $operations) {
  if ($success) {

    // Select all items with uuid.
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'gc_operation_item')
      ->propertyCondition('operation_uuid', $results['uuid'])
      ->execute();
    if (isset($result['gc_operation_item'])) {
      $operation_items = \Drupal::entityManager()
        ->getStorage('gc_operation_item');
      $success_counter = 0;
      foreach ($operation_items as $operation_item) {
        if ($operation_item->status === 'Success') {
          $success_counter++;
        }
      }
      $unsuccessful = count($result['gc_operation_item']) - $success_counter;
      drupal_set_message(\Drupal::translation()
        ->formatPlural($success_counter, '1 item was updated successfully.', '@count items were updated successfully.'));
      if ($unsuccessful > 0) {
        drupal_set_message(\Drupal::translation()
          ->formatPlural($unsuccessful, '1 item was not updated. Check errors below.', '@count items were not updated. Check errors below.'), 'error');
      }
    }
    drupal_goto('admin/content/update/result/' . $results['uuid']);
  }
  else {
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array(
      '@operation' => $error_operation[0],
      '@args' => print_r($error_operation[0], TRUE),
    )), 'error');
  }
}

/**
* @TODO: function comment!!!
*/
function gc_views_vbo_upload($form, &$form_state) {
  $uuid = _gc_uuid_generate();
  $operation = \Drupal::entityManager()
    ->getStorage('gc_operation')
    ->create(array(
    'uuid' => $uuid,
    'type' => 'upload',
  ));
  $operation
    ->save();
  $operations = array();
  $nodes = \Drupal::entityManager()
    ->getStorage('node')
    ->loadMultiple($form_state['selection']);
  foreach ($nodes as $node) {
    $operations[] = array(
      'gc_upload_process',
      array(
        $node,
        $uuid,
      ),
    );
  }
  $batch = array(
    'title' => t('Uploading content ...'),
    'operations' => $operations,
    'finished' => 'gc_upload_finished',
    'init_message' => t('Upload is starting ...'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('An error occurred during processing'),
  );
  batch_set($batch);
}

/**
* Batch operation callback.
*
* We are doing upload operation here.
*/
function gc_upload_process($entity, $uuid, &$context) {

  // 1. Load template from remote
  // 2. Compare local and remote template
  // 3. If templates are same, load node from remote.
  // 4. Set values based on mapping.
  $mapping = \Drupal::entityManager()
    ->getStorage('gc_mapping', array(
    $entity->gc_mapping_id,
  ));
  $mapping = reset($mapping);
  $tmp_obj = new GatherContent\Template();
  $remote_template = $tmp_obj
    ->getTemplate($mapping->gc_template_id);
  $cont_obj = new GatherContent\Content();
  $remote_node = $cont_obj
    ->getContent($entity->gc_id);
  $config = $remote_node->config;
  $mapping_data = unserialize($mapping->data);
  $operation_item = \Drupal::entityManager()
    ->getStorage('gc_operation_item')
    ->create(array(
    'operation_uuid' => $uuid,
    'item_status' => $remote_node->status->data->name,
    'item_status_color' => $remote_node->status->data->color,
    'template_name' => $remote_template->name,
    'item_name' => $remote_node->name,
    'gc_id' => $entity->gc_id,
    'nid' => $entity->nid,
  ));
  if ($remote_template->config == unserialize($mapping->template)->config) {
    $node = entity_metadata_wrapper('node', $entity);
    try {
      foreach ($config as &$pane) {
        $is_translatable = \Drupal::moduleHandler()
          ->moduleExists('entity_translation') && entity_translation_node_supported_type($mapping->content_type) && $mapping_data[$pane->name]['language'] != \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
        if ($is_translatable) {
          $language = $mapping_data[$pane->name]['language'];
        }
        else {
          $language = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
        }
        foreach ($pane->elements as &$field) {
          if (isset($mapping_data[$pane->name]['elements'][$field->name]) && !empty($mapping_data[$pane->name]['elements'][$field->name])) {
            $local_field_name = $mapping_data[$pane->name]['elements'][$field->name];
            if ($mapping_data[$pane->name]['type'] === 'content') {
              $field_info = field_info_field($local_field_name);
              $is_translatable = $is_translatable && $field_info['translatable'];
              switch ($field->type) {
                case 'files':

                  // There is currently no API for manipulating with files.
                  break;
                case 'choice_radio':
                  $option_names = array();
                  foreach ($field->options as &$option) {

                    // Set selected to false for each option.
                    $option->selected = FALSE;
                    $option_names[] = $option->name;
                  }

                  // Fetch local selected option.
                  if ($is_translatable) {
                    $selected = $node
                      ->language($language)->{$local_field_name}
                      ->value();
                  }
                  else {
                    $selected = $node->{$local_field_name}
                      ->value();
                  }
                  if (!in_array($selected, $option_names)) {

                    // If it's other, then find that option in remote.
                    foreach ($field->options as &$option) {
                      if (isset($option->value)) {
                        $option->selected = TRUE;
                        $option->value = $selected;
                      }
                    }
                  }
                  else {

                    // If it's checkbox, find it by remote option name,
                    // which should be same.
                    foreach ($field->options as &$option) {
                      if ($option->name == $selected) {
                        $option->selected = TRUE;
                      }
                    }
                  }
                  break;
                case 'choice_checkbox':
                  foreach ($field->options as &$option) {

                    // Set selected to false for each option.
                    $option->selected = FALSE;
                  }

                  // Fetch local selected option.
                  if ($is_translatable) {
                    $selected = $node
                      ->language($language)->{$local_field_name}
                      ->value();
                  }
                  else {
                    $selected = $node->{$local_field_name}
                      ->value();
                  }

                  // If it's checkbox, find it by remote option name,
                  // which should be same.
                  foreach ($field->options as &$option) {
                    if (isset($selected[$option->name])) {
                      $option->selected = TRUE;
                    }
                  }
                  break;
                case 'section':

                  // We don't upload this because this field shouldn't be
                  // edited.
                  break;
                default:
                  if ($local_field_name === 'title') {
                    $field->value = $node->title
                      ->value();
                  }
                  else {
                    if ($is_translatable) {
                      if (title_field_replacement_enabled('node', $mapping->content_type, 'title') && title_field_replacement_info('node', 'title')['field']['field_name'] === $local_field_name) {
                        $field->value = $node
                          ->language($language)->{$local_field_name}
                          ->value();
                      }
                      else {
                        $field->value = $node
                          ->language($language)->{$local_field_name}->value
                          ->value();
                      }
                    }
                    else {
                      $field->value = $node->{$local_field_name}->value
                        ->value();
                    }
                  }
                  break;
              }
            }
            elseif ($mapping_data[$pane->name]['type'] === 'metatag') {
              if (\Drupal::moduleHandler()
                ->moduleExists('metatag') && metatag_entity_supports_metatags('node', $mapping->content_type)) {
                if ($is_translatable) {
                  $field->value = $node
                    ->language($language)->metatags->{$local_field_name}
                    ->value();
                }
                else {
                  $field->value = $node->metatags->{$local_field_name}
                    ->value();
                }
              }
              else {
                throw new Exception("Metatag module not enabled or entity doesn't support\nmetatags while trying to map values woth metatag content.");
              }
            }
          }
          else {
            $operation_item->status = "System error, please contact you administrator.";
            $operation_item
              ->save();
          }
        }
      }
      if ($cont_obj
        ->postContent($entity->gc_id, $config)) {
        $operation_item->status = "Success";
        $operation_item
          ->save();
      }
      else {
        $operation_item->status = 'Mapping doesn\'t match';
        $operation_item
          ->save();
      }
    } catch (\Exception $e) {
      \Drupal::logger('gc_import')
        ->error(print_r($e, TRUE), array());
      $operation_item->status = 'Mapping doesn\'t match';
      $operation_item
        ->save();
    }
  }
  else {
    $operation_item->status = 'Mapping doesn\'t match';
    $operation_item
      ->save();
  }
  $context['results']['uuid'] = $uuid;
}

/**
* Finished callback.
*
* @inheritdoc
*/
function gc_upload_finished($success, $results, $operations) {
  if ($success) {

    // Select all items with uuid.
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'gc_operation_item')
      ->propertyCondition('operation_uuid', $results['uuid'])
      ->execute();
    if (isset($result['gc_operation_item'])) {
      $operation_items = \Drupal::entityManager()
        ->getStorage('gc_operation_item');
      $success_counter = 0;
      foreach ($operation_items as $operation_item) {
        if ($operation_item->status === 'Success') {
          $success_counter++;
        }
      }
      $unsuccessful = count($result['gc_operation_item']) - $success_counter;
      drupal_set_message(\Drupal::translation()
        ->formatPlural($success_counter, '1 item was uploaded successfully.', '@count items were uploaded successfully.'));
      if ($unsuccessful > 0) {
        drupal_set_message(\Drupal::translation()
          ->formatPlural($unsuccessful, '1 item was not uploaded. Check errors below.', '@count items were not uploaded. Check errors below.'), 'error');
      }
    }
    drupal_goto('admin/content/upload/result/' . $results['uuid']);
  }
  else {
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array(
      '@operation' => $error_operation[0],
      '@args' => print_r($error_operation[0], TRUE),
    )), 'error');
  }
}

/**
* Implements hook_form_alter().
*
* We are adding some classes here.
*/
function gc_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  switch ($form_id) {
    case 'gc_import_form_select':
      $form['#attributes']['class'][] = 'form-select-import';
      break;
    case 'gc_mapping_edit_form':
      $form['#attributes']['class'][] = 'form-gc-mapping';
      break;
  }
}

/**
* Implements hook_libraries_info().
*/
function gc_libraries_info() {
  $libraries['tablesorter-mottie'] = array(
    'name' => "Mottie's tablesorter",
    'vendor url' => 'https://github.com/Mottie/tablesorter',
    'download url' => 'https://github.com/Mottie/tablesorter/archive/master.zip',
    'version callback' => '_gathercontent_tablesorter_mottie_version',
    'files' => array(
      'js' => array(
        'dist/js/jquery.tablesorter.min.js',
      ),
    ),
  );
  return $libraries;
}

/**
* Custom version callback for tablesorter jquery plugin library.
*
* @return string
*   Version of the installed jquery.tablesorter plugin.
*/
function _gathercontent_tablesorter_mottie_version() {
  $lib_version = FALSE;
  $lib_path = libraries_get_path('tablesorter-mottie');
  $lib_packages_json = file_get_contents($lib_path . '/package.json');
  if ($lib_packages_json !== FALSE) {
    $lib_packages_info = json_decode($lib_packages_json, TRUE);
    if (is_array($lib_packages_info) && isset($lib_packages_info['version'])) {
      $lib_version = $lib_packages_info['version'];
    }
  }
  return $lib_version;
}

/**
* Implements template_preprocess_views_view_table().
*/
function gc_preprocess_views_view_table(&$variables) {
  $view = $variables['view'];
  switch ("{$view->name}:{$view->current_display}:{$view->plugin_name}") {
    case 'update:page:table':
    case 'update:page_1:table':

      // Libraries module and tablesorter plugin are optional.
      // If tablesorter available, add it to the scope.
      if (\Drupal::moduleHandler()
        ->moduleExists('libraries')) {
        $library = libraries_detect('tablesorter-mottie');
        if ($library['installed']) {
          $gc_module_path = drupal_get_path('module', 'gc');
          $variables['classes_array'][] = 'tablesorter-enabled';

          // @FIXME
          // The Assets API has totally changed. CSS, JavaScript, and libraries are now
          // attached directly to render arrays using the #attached property.
          //
          //
          // @see https://www.drupal.org/node/2169605
          // @see https://www.drupal.org/node/2408597
          // drupal_add_js($gc_module_path . '/js/gc-tablesorter.js');
          // @FIXME
          // The Assets API has totally changed. CSS, JavaScript, and libraries are now
          // attached directly to render arrays using the #attached property.
          //
          //
          // @see https://www.drupal.org/node/2169605
          // @see https://www.drupal.org/node/2408597
          // drupal_add_js(array(
          //             'gc' => array(
          //               'tablesorterOptionOverrides' => array(
          //                 'headers' => array(
          //                   '0' => array(
          //                     'sorter' => FALSE,
          //                   ),
          //                   '9' => array(
          //                     'sorter' => FALSE,
          //                   ),
          //                   '10' => array(
          //                     'sorter' => FALSE,
          //                   ),
          //                 ),
          //               ),
          //             ),
          //           ), 'setting');
          libraries_load('tablesorter-mottie');
        }
      }
      break;
    case 'import_result_page:page:table':
    case 'import_result_page:page_1:table':
    case 'import_result_page:page_2:table':

      // Libraries module and tablesorter plugin are optional.
      // If tablesorter available, add it to the scope.
      if (\Drupal::moduleHandler()
        ->moduleExists('libraries')) {
        $library = libraries_detect('tablesorter-mottie');
        if ($library['installed']) {
          $gc_module_path = drupal_get_path('module', 'gc');
          $variables['classes_array'][] = 'tablesorter-enabled';

          // @FIXME
          // The Assets API has totally changed. CSS, JavaScript, and libraries are now
          // attached directly to render arrays using the #attached property.
          //
          //
          // @see https://www.drupal.org/node/2169605
          // @see https://www.drupal.org/node/2408597
          // drupal_add_js($gc_module_path . '/js/gc-tablesorter.js');
          // @FIXME
          // The Assets API has totally changed. CSS, JavaScript, and libraries are now
          // attached directly to render arrays using the #attached property.
          //
          //
          // @see https://www.drupal.org/node/2169605
          // @see https://www.drupal.org/node/2408597
          // drupal_add_js(array(
          //             'gc' => array(
          //               'tablesorterOptionOverrides' => array(
          //                 'headers' => array(
          //                   '4' => array(
          //                     'sorter' => FALSE,
          //                   ),
          //                   '5' => array(
          //                     'sorter' => FALSE,
          //                   ),
          //                 ),
          //               ),
          //             ),
          //           ), 'setting');
          libraries_load('tablesorter-mottie');
        }
      }
      break;
  }
}

Functions

Namesort descending Description
gc_form_alter Implements hook_form_alter().
gc_libraries_info Implements hook_libraries_info().
gc_preprocess_views_view_table Implements template_preprocess_views_view_table().
gc_update_finished Finished callback.
gc_update_process Batch operation callback.
gc_upload_finished Finished callback.
gc_upload_process Batch operation callback.
gc_views_bulk_operations_form_alter Implements hook_views_bulk_operations_form_alter().
gc_views_pre_render Implements hook_views_pre_render().
gc_views_vbo_update Submit callback for `views_bulk_operations_confirm_form`.
gc_views_vbo_upload @TODO: function comment!!!
_gathercontent_tablesorter_mottie_version Custom version callback for tablesorter jquery plugin library.