home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (uc_catalog.install)

This example Drupal source code file (uc_catalog.install) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, catalog, directory, field, field_name, file, function, if, instance, label, php, type, uc_catalog_image, vid

The uc_catalog.install Drupal example source code

<?php

/**
 * @file
 * Install, update and uninstall functions for the uc_catalog module.
 */

/**
 * Implements hook_install().
 *
 * Creates a Catalog vocabulary or uses one by that name that may already exist.
 */
function uc_catalog_install() {
  $t = get_t();
  // Find possible Product Catalog vocabulary.
  $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = :name", array(':name' => $t('Catalog')))->fetchField();

  // If none, create one.
  if (!$vid) {
    $vocabulary = new stdClass();
    $vocabulary->name = $t('Catalog');
    $vocabulary->description = '';
    $vocabulary->hierarchy = 1;
    $vocabulary->module = 'uc_catalog';
    $vocabulary->machine_name = 'catalog';

    taxonomy_vocabulary_save($vocabulary);
    $vid = $vocabulary->vid;

    $field = field_info_field('taxonomy_' . $vocabulary->machine_name);
    if (empty($field)) {
      $field = array(
        'field_name' => 'taxonomy_' . $vocabulary->machine_name,
        'type' => 'taxonomy_term_reference',
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
        'settings' => array(
          'allowed_values' => array(
            array(
              'vocabulary' => $vocabulary->machine_name,
              'parent' => 0,
            ),
          ),
        ),
      );

      field_create_field($field);
    }
  }

  uc_catalog_add_image_field();

  variable_set('uc_catalog_vid', $vid);
  variable_set('uc_catalog_name', $t('Catalog'));
}

/**
 * Implements hook_uninstall().
 */
function uc_catalog_uninstall() {
  if ($vid = variable_get('uc_catalog_vid', 0)) {
    $vocab = taxonomy_vocabulary_load($vid);
    drupal_set_message(t('The Ubercart Catalog vocabulary has not been deleted. If you need to delete it, <a href="!url">please do so manually</a>.', array('!url' => url('admin/structure/taxonomy/' . $vocab->machine_name . '/edit'))), 'warning');
  }

  db_delete('variable')
    ->condition('name', 'uc_catalog_%', 'LIKE')
    ->execute();
}

/**
 * Implements hook_update_dependencies().
 */
function uc_catalog_update_dependencies() {
  $dependencies['uc_catalog'][7000] = array(
    'taxonomy' => 7002,
  );
  $dependencies['uc_catalog'][7001] = array(
    'system' => 7059,
  );

  // Try to alter vocabulary machine name before taxonomy reference field is
  // created. If dependency is honored, uc_catalog_update_7003 is a no-op.
  $dependencies['taxonomy'][7004] = array(
    'uc_catalog' => 7002,
  );

  return $dependencies;
}

/**
 * Implements hook_update_last_removed().
 */
function uc_catalog_update_last_removed() {
  return 9;
}

/**
 * Sets up a default term image for the Catalog.
 */
function uc_catalog_update_7000() {
  if (!module_exists('image')) {
    module_enable(array('file', 'image'), FALSE);
  }

  // Copied from uc_catalog_add_image_field(), since the module won't be
  // enabled during an upgrade.
  $field = field_info_field('uc_catalog_image');

  if (!$field) {
    $field = array(
      'field_name' => 'uc_catalog_image',
      'type' => 'image',
    );

    field_create_field($field);
  }

  $instance = field_info_instance('taxonomy_term', 'uc_catalog_image', 'catalog');

  // Only add the instance if it doesn't exist. Don't overwrite any changes.
  if (!$instance) {
    $label = t('Image');
    $instance = array(
      'field_name' => 'uc_catalog_image',
      'entity_type' => 'taxonomy_term',
      'bundle' => 'catalog',
      'label' => $label,
      'widget' => array(
        'type' => 'image_image',
      ),
      'display' => array(
        'full' => array(
          'label' => 'hidden',
          'type' => 'image',
          'settings' => array(
            'image_link' => 'content',
            'image_style' => 'uc_category',
          ),
        ),
      ),
    );

    field_create_instance($instance);
  }
}

/**
 * Migrates uploaded catalog images to the new {file} table.
 */
function uc_catalog_update_7001(&$sandbox) {
  if (!db_table_exists('uc_catalog_images')) {
    return;
  }

  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_fid'] = 0;
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT fid) FROM {uc_catalog_images}")->fetchField();
  }

  $limit = 500;

  // The old {files} tables still exists.  We migrate core data from upload
  // module, but any contrib module using it will need to do its own update.
  $result = db_query_range('SELECT tid, f.fid, uid, u.filename, u.filepath AS uri, u.filemime, u.filesize, status, timestamp FROM {files} f INNER JOIN {uc_catalog_images} u ON u.fid = f.fid WHERE f.fid > :current ORDER BY f.fid', 0, $limit, array(':current' => $sandbox['current_fid']), array('fetch' => PDO::FETCH_ASSOC));

  // We will convert filepaths to uri using the default schmeme
  // and stripping off the existing file directory path.
  $basename = variable_get('file_directory_path', conf_path() . '/files');
  $scheme = variable_get('file_default_scheme', 'public') . '://';
  $fids = array();

  foreach ($result as $file) {
    // Get term id for the image.
    $tid = $file['tid'];
    // Don't break the insert query with extra data.
    unset($file['tid']);

    $file['uri'] = $scheme . str_replace($basename, '', $file['uri']);
    $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
    db_merge('file_managed')
      ->key(array('fid' => $file['fid']))
      ->fields(array(
        'uid' => $file['uid'],
        'filename' => $file['filename'],
        'uri' => $file['uri'],
        'filemime' => $file['filemime'],
        'filesize' => $file['filesize'],
        'status' => $file['status'],
        'timestamp' => $file['timestamp'],
      ))
      ->execute();
    $fids[] = $file['fid'];

    // Add the usage entry for the file.
    file_usage_add((object) $file, 'file', 'taxonomy_term', $tid);

    $term = (object) array(
      'tid' => $tid,
      'uc_catalog_image' => array(
        LANGUAGE_NONE => array(
          0 => array(
            'fid' => $file['fid'],
          ),
        ),
      ),
    );

    _update_7000_field_sql_storage_write('taxonomy_term', 'catalog', $term->tid, NULL, 'uc_catalog_image', $term->uc_catalog_image);

    $sandbox['progress']++;
    $sandbox['current_fid'] = $file['fid'];
    $sandbox['message'] = check_plain($file['filename']);
  }
  // TODO: delete the found fids from {files}?

  if ($sandbox['progress'] < $sandbox['max']) {
    $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
  }
  else {
    $sandbox['#finished'] = 1;
  }
}

/**
 * Sets Catalog vocbulary machine name.
 */
function uc_catalog_update_7002() {
  db_query("UPDATE {taxonomy_vocabulary} SET machine_name = :name WHERE vid = :vid", array(':name' => 'catalog', ':vid' => variable_get('uc_catalog_vid', 0)));
}

/**
 * Renames Catalog taxonomy reference field created in taxonomy_update_7004().
 */
function uc_catalog_update_7003() {
  $vid = variable_get('uc_catalog_vid', 0);
  $field_name = 'taxonomy_vocabulary_' . $vid;
  if ($field = field_info_field($field_name)) {
    $field['field_name'] = 'taxonomy_catalog';
    unset($field['id']);
    field_create_field($field);

    $label = variable_get('uc_catalog_name', t('Catalog'));
    foreach ($field['bundles'] as $type) {
      $instance = array(
        'field_name' => 'taxonomy_catalog',
        'entity_type' => 'node',
        'bundle' => $type,
        'label' => $label,
        'settings' => array(
          'allowed_values' => array(
            array(
              'vid' => $vid,
              'parent' => '0',
            ),
          ),
        ),
      );

      field_create_instance($instance);
    }

    if (isset($field['id'])) {
      field_delete_field($field_name);
    }
  }
}

/**
 * Delete unused catalog description.
 */
function uc_catalog_update_7004() {
  variable_del('uc_catalog_description');
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal uc_catalog.install source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.