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

Drupal example source code file (userreference.install)

This example Drupal source code file (userreference.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

abort, array, column, content, database, empty, field, foreach, function, if, php, ret, return, userreference

The userreference.install Drupal example source code

<?php
// $Id: userreference.install,v 1.23.2.10 2009/11/07 11:04:15 markuspetrux Exp $

/**
 * @file
 * Implementation of hook_install().
 */
function userreference_install() {
  drupal_load('module', 'content');
  content_notify('install', 'userreference');
}

/**
 * Implementation of hook_uninstall().
 */
function userreference_uninstall() {
  drupal_load('module', 'content');
  content_notify('uninstall', 'userreference');
}

/**
 * Implementation of hook_enable().
 *
 * Notify content module when this module is enabled.
 */
function userreference_enable() {
  drupal_load('module', 'content');
  content_notify('enable', 'userreference');
}

/**
 * Implementation of hook_disable().
 *
 * Notify content module when this module is disabled.
 */
function userreference_disable() {
  drupal_load('module', 'content');
  content_notify('disable', 'userreference');
}

function userreference_update_last_removed() {
  return 4;
}

/**
 * Placeholder update to set newly installed versions to the latest update number.
 */
function userreference_update_6000() {
  if ($abort = content_check_update('userreference')) {
    return $abort;
  }

  $ret = array();
  return $ret;
}

/**
 * Create an index by user reference column for all fields.
 */
function userreference_update_6001(&$sandbox) {
  include_once('./'. drupal_get_path('module', 'content') .'/content.install');
  drupal_load('module', 'content');

  $ret = array();

  if (!isset($sandbox['progress'])) {
    if ($abort = content_check_update('userreference')) {
      return $abort;
    }

    // Get the latest cache values and schema.
    content_clear_type_cache(TRUE, TRUE);
    $types = content_types_install();

    if (empty($types)) {
      return $ret;
    }

    $sandbox['fields'] = array();
    foreach ($types as $type_name => $fields) {
      foreach ($fields as $field) {
        if ($field['type'] == 'userreference') {
          $sandbox['fields'][] = $field;
        }
      }
    }

    if (empty($sandbox['fields'])) {
      return $ret;
    }

    $sandbox['progress'] = 0;
    $sandbox['visited'] = array();
  }

  $field = $sandbox['fields'][$sandbox['progress']];

  // We only want to process a field once -- if we hit it a second time,
  // that means it's its own table and it should have already been updated.
  if (!in_array($field['field_name'], $sandbox['visited'])) {
    $db_info = content_database_info($field);
    $table = $db_info['table'];
    $attributes = $db_info['columns']['uid'];
    $column = $attributes['column'];
    if (!content_db_index_exists($table, $column)) {
      db_add_index($ret, $table, $column, array($column));
    }
    $sandbox['visited'][] = $field['field_name'];
  }

  $sandbox['progress']++;
  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);

  return $ret;
}

/**
 * Convert 'referenceable_status' option from array to integer to match the
 * change in the field settings form where the element has been changed from
 * a checkboxes element (array) to a radios element (integer).
 *
 * Reference: @link http://drupal.org/node/416134 @endlink
 */
function userreference_update_6002() {
  $ret = array();

  drupal_load('module', 'content');

  $result = db_query("SELECT field_name, global_settings FROM {". content_field_tablename() ."} WHERE type = 'userreference'");
  while ($userreference = db_fetch_object($result)) {
    $global_settings = unserialize($userreference->global_settings);

    if (isset($global_settings['referenceable_status']) && is_array($global_settings['referenceable_status'])) {
      $referenceable_status = array_filter($global_settings['referenceable_status']);
      $global_settings['referenceable_status'] = (!empty($referenceable_status) ? 1 : '');

      // We can't use update_sql() here because of curly braces in serialized
      // array.
      db_query("UPDATE {". content_field_tablename() ."} SET global_settings = '%s' WHERE field_name = '%s'", serialize($global_settings), $userreference->field_name);
      $ret[] = array(
        'success' => TRUE,
        'query' => t("The 'referenceable_status' option for %field has been fixed.", array('%field' => $userreference->field_name)),
      );
    }
  }

  // Rebuild content caches only if necessary.
  if (!empty($ret)) {
    content_clear_type_cache();
  }

  return $ret;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal userreference.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.