You are here

node_field.module in Node Field 7.2

This module provide ability to add extra fields to any single node.

File

node_field.module
View source
<?php

/**
 * @file
 * This module provide ability to add extra fields to any single node.
 */

/**
 * Requirements.
 */
require_once 'model/node_field.db.inc';
require_once 'includes/node_field.field.inc';
require_once 'includes/node_field.api.inc';

// Constants.
define('NODE_FIELD_OPTIONS_FIELD_DESCRIPTION', t('Possible values this field can contain. Enter one value per line in the key|label format.'));

/**
 * Implements hook_menu().
 */
function node_field_menu() {
  $items = [];
  if (module_exists('node_field_gmap')) {
    $items['admin/config/content/node-field'] = [
      'title' => 'Node field settings',
      'page callback' => 'drupal_get_form',
      'page arguments' => [
        'node_field_admin_settings_form',
      ],
      'access arguments' => [
        'administer modules',
      ],
      'type' => MENU_NORMAL_ITEM,
      'file' => 'includes/pages/node_field.admin.inc',
    ];
  }
  $items['node/%node_field_menu_loader_node/node-field'] = [
    'title' => 'Node fields',
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'node_field_node_field_form',
      1,
    ],
    'access callback' => 'node_access',
    'access arguments' => [
      'update',
      1,
    ],
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'includes/node_field.form.node_field.inc',
  ];
  $items['node/%node_field_menu_loader_node/node-field/%node_field/view'] = [
    'title' => 'Node fields',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  ];
  $items['node/%node_field_menu_loader_node/node-field/%node_field/edit'] = [
    'title callback' => 'node_field_menu_title_callback',
    'title arguments' => [
      3,
      'edit',
    ],
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'node_field_node_field_edit_form',
      1,
      3,
    ],
    'access callback' => 'node_access',
    'access arguments' => [
      'update',
      1,
    ],
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/node_field.form.node_field.inc',
    'weight' => 1,
  ];
  $items['node/%node_field_menu_loader_node/node-field/%node_field/delete'] = [
    'title callback' => 'node_field_menu_title_callback',
    'title arguments' => [
      3,
      'delete',
    ],
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'node_field_node_field_delete_form',
      1,
      3,
    ],
    'access callback' => 'node_access',
    'access arguments' => [
      'update',
      1,
    ],
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/node_field.form.node_field.inc',
    'weight' => 2,
  ];
  return $items;
}

/**
 * Title callback for node field menu items.
 */
function node_field_menu_title_callback($field, $op) {
  if ($op == 'edit') {
    return t('Edit "!title" node field', [
      '!title' => $field['title'],
    ]);
  }
  else {
    return t('Delete "!title" node field', [
      '!title' => $field['title'],
    ]);
  }
}

/**
 * Implements hook_admin_paths().
 */
function node_field_admin_paths() {
  $paths = [
    'node/*/node-field' => TRUE,
    'node/*/node-field/*' => TRUE,
  ];
  return $paths;
}

/**
 * Menu node loader callback.
 */
function node_field_menu_loader_node_load($nid) {
  $node = node_load($nid);

  // Return if it is not a node.
  if (!$node) {
    return FALSE;
  }
  if (user_access('add/edit/delete fields to node')) {
    return $node;
  }
  else {
    return FALSE;
  }
}

/**
 * Menu node_field loader callback.
 */
function node_field_load($fid) {
  if ($fid) {
    return node_field_load_node_field($fid);
  }
  else {
    return FALSE;
  }
}

/**
 * Implements hook_theme().
 */
function node_field_theme() {
  $items = [];
  $items['node_field_field'] = [
    'render element' => 'element',
    'template' => 'theme/tpl/node_field_field',
  ];
  $items['node_field_fields'] = [
    'render element' => 'element',
    'template' => 'theme/tpl/node_field_fields',
  ];
  $items['node_field_node_field_page_form_fields'] = [
    'render element' => 'container',
    'file' => 'theme/node_field.template.inc',
  ];
  $items['node_field_views_node_field'] = [
    'variables' => [
      'node_fields',
    ],
    'file' => 'theme/node_field.theme.inc',
  ];
  return $items;
}

/**
 * Implements hook_field_extra_fields().
 */
function node_field_field_extra_fields() {
  $items = [];
  $options = variable_get('node_field_node_types', []);
  foreach ($options as $option) {
    if (empty($option)) {
      continue;
    }
    $items['node'][$option] = [
      'display' => [
        'node_fields' => [
          'label' => t('Node fields'),
          'description' => t('Node fields'),
          'weight' => 0,
        ],
      ],
      'form' => [
        'node_fields' => [
          'label' => t('Node fields'),
          'description' => t('Node fields'),
          'visible' => TRUE,
          'weight' => 0,
        ],
      ],
    ];
  }
  return $items;
}

/**
 * Implements hook_node_view().
 */
function node_field_node_view($node, $view_mode, $langcode) {
  if (user_access('add/edit/delete fields to node')) {

    // Adding node_fields to content.
    $node_fields = node_field_get_node_fields($node);
    $exclude = module_invoke_all('node_field_exclude_node_content', $node);
    if (empty($node_fields) || $exclude) {
      return;
    }
    $node->content['node_fields'] = node_field_node_content_add_node_fields($node_fields);
  }
}

/**
 * Implements hook_node_update().
 *
 * Allows to change node fields param in code.
 * Use node_field_get_node_fields and node_fields_set_node_fields functions.
 */
function node_field_node_update($node) {
  if (user_access('add/edit/delete fields to node')) {
    if (!node_field_is_node_fields_changed($node)) {
      return;
    }
    $node_fields = node_field_get_node_fields($node);
    foreach ($node_fields as $field) {
      if (!empty($field['delete'])) {
        node_field_delete_node_field($field);
      }
      elseif (!empty($field['id'])) {
        node_field_update_node_field($field);
      }
      else {
        node_field_create_node_field($field);
      }
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function node_field_node_delete($node) {
  if (user_access('add/edit/delete fields to node')) {
    $node_fields = node_field_get_node_fields($node);
    if ($node_fields) {
      foreach ($node_fields as $node_field) {
        node_field_delete_node_field($node_field);
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function node_field_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];
  if (user_access('add/edit/delete fields to node')) {
    if (empty($node->nid)) {
      return;
    }
    form_load_include($form_state, 'inc', 'node_field', 'includes/node_field.form');
    node_field_node_form_edit($form, $form_state);
  }
}

/**
 * Implements hook_node_submit().
 */
function node_field_node_submit($node, $form, &$form_state) {
  if (isset($form_state['values']['node_fields'])) {
    drupal_alter('node_field_node_submit', $form_state);
    foreach ($form_state['values']['node_fields'] as $fid => $value) {
      if (!empty($form_state['node_fields'][$fid])) {
        $field = $form_state['node_fields'][$fid];

        // Text field with formats.
        if ($field['type'] == 'text_long' && is_array($value)) {
          $field['value'] = $value['value'];
          $field['settings']['text_format'] = $value['format'];
        }
        else {
          $field['value'] = $value;
        }
        node_field_update_node_field($field);
      }
    }
  }
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function node_field_taxonomy_term_delete($term) {

  // Delete taxonomy node field values if term was deleted.
  $query_fields = [
    'value' => $term->tid,
    'type' => 'taxonomy',
  ];
  $node_fields = node_field_db_field_select($query_fields);
  foreach ($node_fields as $item) {
    $item['value'] = NULL;
    drupal_alter('node_field_term_default_value', $item);
    node_field_update_node_field($item);
  }
}

/**
 * Implements hook_preprocess_THEME().
 */
function node_field_preprocess_node_field_field(&$vars) {

  // Default element.
  $element = $vars['element'];

  // Class.
  $class = [];
  $class[] = $element['#node_field']['show_title'] ? 'with-title' : 'without-title';
  $class[] = 'type-' . $element['#node_field']['type'];
  $vars['class'] = implode(' ', $class);

  // Show title.
  $vars['show_title'] = $element['#node_field']['show_title'];
  $vars['hidden'] = $element['#node_field']['hidden'];
  $vars['title'] = check_plain($element['#node_field']['title']);
  $vars['value'] = $element['#node_field']['value'];
}

/**
 * Implements hook_preprocess_THEME().
 */
function node_field_preprocess_node_field_fields(&$vars) {
  $element = $vars['element'];
  $vars['children'] = $element['#children'];
}

/**
 * Preprocesses variables for node_field_views_node_field theme.
 */
function template_preprocess_node_field_views_node_field(&$vars) {
  foreach ($vars['node_fields'] as $key => $node_field) {
    $vars['node_fields'][$key]['title'] = check_plain($node_field['title']);
  }
}

/**
 * Implements hook_permission().
 */
function node_field_permission() {
  $items = [];
  $items['add classes to node fields'] = [
    'title' => t('Add classes to node fields'),
    'description' => t('Allow to set additional classes to node fields.'),
  ];
  $items['add/edit/delete fields to node'] = [
    'title' => t('Add/Edit/Delete fields to node'),
    'description' => t('Allow to add/edit/delete fields to node.'),
  ];
  return $items;
}

/**
 * Implements hook_node_field_settings().
 */
function node_field_node_field_settings_alter($node_field, &$form) {
  if (user_access('add classes to node fields')) {
    $form['node_field_class'] = [
      '#type' => 'textfield',
      '#title' => t('Additional Field Class(es)'),
      '#default_value' => !empty($node_field['settings']['node_field_class']) ? $node_field['settings']['node_field_class'] : NULL,
      '#size' => 60,
      '#weight' => 25,
      '#description' => t('Specify class(es) that you want to add to the field element. It (they) will be added to the class attribute of the element. Multiple classes must be separated by space.'),
    ];
  }
}

/**
 * Implements hook_views_api().
 */
function node_field_views_api() {
  $path = drupal_get_path('module', 'node_field');
  return [
    'api' => '3.0',
    'path' => $path . '/views',
  ];
}

/**
 * Implements hook_clone_node_alter().
 *
 * Node clone module support.
 */
function node_field_clone_node_alter(&$node, $context) {

  // Save id of original node to be used on node insert.
  $node->node_field_clone = $context['original_node']->nid;
}

/**
 * Implements hook_node_insert().
 */
function node_field_node_insert($node) {

  // Copy node fields from original node to cloned node.
  if (!empty($node->node_field_clone)) {
    $original_node = node_load($node->node_field_clone);
    node_field_copy_node_fields($original_node, $node);
  }
}

/**
 * Implements hook_form_alter().
 */
function node_field_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']['#value']) && $form_id == $form['type']['#value'] . '_node_form' && $form['nid']['#value'] == NULL) {
    drupal_set_message(t('You can add custom extra fields after creating this node by going to the Tab "Node fields".'), 'status');
  }
}