View source
<?php
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.'),
);
}
}
function nr_fields_nr_settings_enable_private_fields_submit($form, &$form_state) {
$node = $form['#node'];
$settings = $node->registration;
$settings
->update(array(
'private_fields' => 1,
));
$bundle = 'node_' . $node->nid;
$instances = field_info_instances('node_registration', $node->type);
foreach ($instances as $instance) {
$instance['bundle'] = $bundle;
field_create_instance($instance);
}
$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,
)));
entity_info_cache_clear();
menu_rebuild();
}
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');
$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++;
}
$new_fields = array();
foreach ($fields as $field_name => $field) {
if ($field['field'] == 'nr_fields_private_fields') {
if ($node) {
$bundle = 'node_' . $node->nid;
$instances = field_info_instances('node_registration', $bundle);
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;
}
}
$view->display_handler
->set_option('fields', $new_fields);
}
}
function nr_fields_views_data() {
$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;
}