You are here

rh_profile2.module in Rabbit Hole 7.2

Main module file for Rabbit Hole Profile2 module.

This module will add the Rabbit Hole functionality to Profile2 entities.

File

modules/rh_profile2/rh_profile2.module
View source
<?php

/**
 * @file
 * Main module file for Rabbit Hole Profile2 module.
 *
 * This module will add the Rabbit Hole functionality to Profile2 entities.
 */

/**
 * Implements hook_rabbit_hole().
 */
function rh_profile2_rabbit_hole() {
  return array(
    'rh_profile2' => array(
      'entity type' => 'profile2',
      'base table' => 'profile',
      'view path' => '%profile2_by_uid/view',
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * This will add Rabbit Hole options to the Profile2 type form. These settings
 * will be used as default for every Profile of this Profile type.
 *
 * @param $form
 * @param $form_state
 */
function rh_profile2_form_profile2_type_form_alter(&$form, $form_state) {
  if (!user_access('administer rh_profile2') || empty($form_state['profile2_type']->type)) {

    // The user doesn't have access.
    return;
  }

  // Load the Rabbit Hole form, and add an extra javascript file that's needed
  // for the fieldset summary.
  rabbit_hole_form($form, 'profile2', $form_state['profile2_type']->type, 'rh_profile2');
  if (isset($form['rabbit_hole'])) {
    $form['#attached']['js'][] = drupal_get_path('module', 'rh_profile2') . '/rh-profile2.js';
  }
}

/**
 * Submit callback for the bundle form.
 *
 * This will set the values of the variables.
 *
 * @param $form
 * @param $form_state
 */
function rh_profile2_bundle_form_submit($form, $form_state) {
  $values = $form_state['values'];

  // Set the values of the variables.
  variable_set('rh_profile2_override_' . $values['type'], $values['rh_profile2_override']);
  variable_set('rh_profile2_action_' . $values['type'], $values['rh_profile2_action']);
  variable_set('rh_profile2_redirect_' . $values['type'], $values['rh_profile2_redirect']);
  variable_set('rh_profile2_redirect_response_' . $values['type'], $values['rh_profile2_redirect_response']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * This will add Rabbit Hole options to the Profile edit form. The user will be able to
 * override the default Rabbit Hole options.
 *
 * @param $form
 * @param $form_state
 */
function rh_profile2_form_profile2_form_alter(&$form, $form_state) {
  if (!user_access('administer rh_profile2')) {

    // The user doesn't have access.
    return;
  }

  // Load the Rabbit Hole form, and add an extra javascript file that's needed
  // for the fieldset summary.
  foreach (element_children($form) as $key) {
    $element = $form[$key];
    if (isset($element['#entity_type']) && $element['#entity_type'] == 'profile2') {
      rabbit_hole_form($form, 'profile2', $element['#bundle'], 'rh_profile2', $form_state['profiles'][$element['#bundle']]);
      if (isset($form['rabbit_hole'])) {
        $form['#attached']['js'][] = drupal_get_path('module', 'rh_profile2') . '/rh-profile2.js';
        array_unshift($form['#submit'], 'rh_profile2_profile2_form_prepare_submit');
        if (!user_access('bypass rh_profile2')) {
          $form['#submit'][] = 'rh_profile2_profile2_form_submit';
        }
      }
    }
  }
}

/**
 * Submit handler to set up entity properties before save.
 *
 * @param $form
 * @param $form_state
 */
function rh_profile2_profile2_form_prepare_submit($form, &$form_state) {
  foreach (element_children($form) as $key) {
    $element = $form[$key];
    if (isset($element['#entity_type']) && $element['#entity_type'] == 'profile2') {
      $profile =& $form_state['profiles'][$element['#bundle']];
      $profile->rh_action = $form_state['values']['rh_action'];
      $profile->rh_redirect = $form_state['values']['rh_redirect'];
      $profile->rh_redirect_response = $form_state['values']['rh_redirect_response'];
    }
  }
}

/**
 * Custom submit function for the Profile edit form.
 *
 * @param $form
 * @param $form_state
 */
function rh_profile2_profile2_form_submit($form, &$form_state) {

  // Get the action. Either the one specified for this Profile, or the default
  // value for the bundle.
  $action = isset($form_state['values']['rh_action']) && $form_state['values']['rh_action'] != RABBIT_HOLE_USE_DEFAULT ? $form_state['values']['rh_action'] : rabbit_hole_get_action_bundle('profile2', $form_state['profile2']->type);

  // If the action says anything else than to display the content, make sure
  // that the user doesn't land on the Profile view page. We'll check if a custom
  // redirect path has been set, otherwise, we'll redirect the user to the edit
  // page again.
  if ($action != RABBIT_HOLE_DISPLAY_CONTENT && $form_state['redirect'] == $form_state['profile2']
    ->path()) {
    $form_state['redirect'] = $form_state['profile2']
      ->path() . '/edit';
  }
}

/**
 * Implements hook_profile2_view().
 *
 * @param $profile
 * @param $view_mode
 * @param $langcode
 */
function rh_profile2_profile2_view($profile, $view_mode, $langcode) {
  if ($view_mode == 'page' && !user_access('bypass rh_profile2')) {
    rabbit_hole_execute('profile2', $profile);
  }
}

/**
 * Implements hook_profile2_type_delete().
 *
 * @param $profile_type
 */
function rh_profile2_profile2_type_delete($profile_type) {

  // Delete variables connected to this bundle.
  rabbit_hole_delete_variables('profile2', $profile_type->type);
}

Functions