You are here

nr_fields.module in Node registration 7

File

submodules/nr_fields/nr_fields.module
View source
<?php

/**
 * Implements hook_form_FORM_ID_alter() for node_registration_registrations_settings_form();
 */
function nr_fields_form_node_registration_registrations_settings_form_alter(&$form, &$form_state) {
  $node = $form['#node'];
  $settings = $node->registration;
  $form['fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('Fields'),
    '#weight' => -20,
  );
  if ($settings->private_fields) {
    $bundle = 'node_' . $node->nid;
    $uri = 'admin/structure/node_registration/manage/' . $bundle . '/fields';
    $form['fields']['link'] = array(
      '#type' => 'link',
      '#href' => $uri,
      '#title' => t('Edit them here.'),
      '#prefix' => t('<strong>This node has its own private fields.</strong>') . ' ',
    );
  }
  else {
    $form['fields']['#collapsible'] = $form['fields']['#collapsed'] = TRUE;
    $form['fields']['override_copy'] = array(
      '#type' => 'submit',
      '#submit' => array(
        'nr_fields_nr_settings_enable_private_fields_submit',
      ),
      '#value' => t('Set up private fields for this node'),
      '#suffix' => ' ' . t('Fields will be copied from the registration type.'),
    );
  }
}

/**
 * Submit handler for node_registration_registrations_settings_form().
 *
 * Enables 'private fields' for this node/event. Creates a new node_registration bundle.
 */
function nr_fields_nr_settings_enable_private_fields_submit($form, &$form_state) {
  $node = $form['#node'];
  $settings = $node->registration;

  // Create new registration bundle.
  $settings
    ->update(array(
    'private_fields' => 1,
  ));

  // Copy fields.
  $bundle = 'node_' . $node->nid;
  $instances = field_info_instances('node_registration', $node->type);
  foreach ($instances as $instance) {
    $instance['bundle'] = $bundle;
    field_create_instance($instance);
  }

  // Notify user.
  $uri = 'admin/structure/node_registration/manage/' . $bundle . '/fields';
  $link = l(t('Edit its fields here.'), $uri);
  drupal_set_message(t('Private fields created. !link', array(
    '!link' => $link,
  )));

  // Reset entities & menu routes.
  entity_info_cache_clear();
  menu_rebuild();
}

/**
 * Implemnts hook_views_pre_view().
 *
 * Dynamically add node registration private fields to views listings.
 */
function nr_fields_views_pre_view($view, $display_id, $args) {
  if ($view->base_table == 'node_registration') {
    $fields = $view->display_handler
      ->get_option('fields');
    $arguments = $view->display_handler
      ->get_option('arguments');

    // Get the node this view is operating on.
    $node = NULL;
    $key = 0;
    foreach ($arguments as $argument) {
      if ($argument['table'] == $view->base_table && $argument['field'] == 'nid' && isset($view->args[$key])) {
        $nid = $view->args[$key];
        $node = node_load($nid);
        break;
      }
      $key++;
    }

    // We iterate over each field. Fields of type nr_fields_private_fields are
    // replaced by the actual private fields of the node.
    $new_fields = array();
    foreach ($fields as $field_name => $field) {
      if ($field['field'] == 'nr_fields_private_fields') {
        if ($node) {

          // Retrieve all the private fields for this node.
          $bundle = 'node_' . $node->nid;
          $instances = field_info_instances('node_registration', $bundle);

          // Each field is added as a Views field.
          foreach ($instances as $instance) {
            $new_field_name = $field['id'] . '_' . $instance['field_name'];
            $new_fields[$new_field_name]['id'] = $instance['field_name'];
            $new_fields[$new_field_name]['table'] = _field_sql_storage_tablename($instance);
            $new_fields[$new_field_name]['field'] = $instance['field_name'];
          }
        }
      }
      else {
        $new_fields[$field_name] = $field;
      }
    }

    // Replace old fields with new fields.
    $view->display_handler
      ->set_option('fields', $new_fields);
  }
}

/**
 * Implements hook_views_data_alter().
 */
function nr_fields_views_data() {

  // Field: Private fields.
  $data['node_registration']['nr_fields_private_fields'] = array(
    'title' => t('Private fields'),
    'help' => t('When rendered, this field will be replaced by all the private fields of the node. No configuration is required. Please be aware this only works for listings operating on a single node (eg. by using the "Node registration: Node nid" contextual filter).'),
    'field' => array(
      'field' => 'registration_id',
      'group' => t('Node registration'),
      'handler' => 'views_handler',
    ),
  );
  return $data;
}