You are here

google_authorship.install in Google Authorship 7

Same filename and directory in other branches
  1. 7.2 google_authorship.install

Adds field to user entity on enable and removes fields on uninstall. Will not delete fields upon disable.

File

google_authorship.install
View source
<?php

/**
 * @file
 * Adds field to user entity on enable and removes fields on uninstall. Will
 * not delete fields upon disable.
 */

/**
 * Implements hook_enable().
 *
 * This will check to see if the field has been created and if not will
 * create it.  * It will then add an instance of that field to the user
 * entity and set various defaults and other settings.
 */
function google_authorship_enable() {

  // Check if our field is not already created.
  if (!field_info_field('field_google_plus_id')) {

    // Creates an array to pass to Drupal's field_create_field function.
    $field = array(
      'field_name' => 'field_google_plus_id',
      'type' => 'text',
      // The maximum number of values this field can store is 1
      'cardinality' => '1',
    );

    // Takes the previous array and creates a new Drupal 7 field.
    field_create_field($field);

    // Create the array to pass to field_create_instance()
    $instance = array(
      'field_name' => 'field_google_plus_id',
      'entity_type' => 'user',
      'label' => 'Google+ ID',
      'description' => t('This is the 21 digit number found in the URL of
                         this user\'s Google+ profile.'),
      'bundle' => 'user',
      // If you don't set the "required" property then the field wont be
      // required by default.
      'settings' => array(
        // Here you inform either or not you want this field showing up on
        // the registration form.
        'user_register_form' => 1,
      ),
      // Sets the input widget type.
      'widget' => array(
        'type' => 'textfield',
        'weight' => '1',
        'settings' => array(
          // Sets the max size of the input to 21, the size of Google+ IDs.
          'size' => '21',
        ),
      ),
      'display' => array(
        'default' => array(
          // Hide the Google+ ID on the user page.
          'label' => 'hidden',
          // Attempts to set the weight to 2, but Drupal's weight system has
          // a bug.
          'weight' => '2',
        ),
      ),
    );

    // Takes the previous array and creates a new Drupal 7 instance on the
    // user bundle.
    field_create_instance($instance);
  }
}

/**
 * Implements hook_uninstall().
 *
 * This will delete the Google+ ID field and all its instances from the user
 * bundle when the module is uninstalled.
 */
function google_authorship_uninstall() {
  if (field_info_field('field_google_plus_id')) {
    field_delete_field('field_google_plus_id');
  }
}

Functions