You are here

video_customfields.module in Video 6

Same filename and directory in other branches
  1. 5 plugins/video_customfields/video_customfields.module

Enable addition of custom fileds on video nodes created by video module.

@author Fabio Varesano <fvaresano at yahoo dot it> porting to Drupal 6 @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw@gmail.com> @todo

File

plugins/video_customfields/video_customfields.module
View source
<?php

/**
 * @file
 * Enable addition of custom fileds on video nodes created by video module.
 *
 * @author Fabio Varesano <fvaresano at yahoo dot it>
 * porting to Drupal 6
 * @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw@gmail.com>
 * @todo
 */

/**
 * Implementation of hook_help().
 */
function video_customfields_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/modules#description':
      return t('Enable addition of custom fileds on video nodes created by video module.');
  }
}

/**
 * Implementation of hook_perm().
 */
function video_customfields_perm() {
  return array(
    'insert custom fields',
  );
}

/**
 * Implementation of hook_menu().
 */
function video_customfields_menu() {
  $items = array();

  /* TODO
     Non menu code that was placed in hook_menu under the '!$may_cache' block
     so that it could be run during initialization, should now be moved to hook_init.
     Previously we called hook_init twice, once early in the bootstrap process, second
     just after the bootstrap has finished. The first instance is now called boot
     instead of init.

     In Drupal 6, there are now two hooks that can be used by modules to execute code
     at the beginning of a page request. hook_boot() replaces hook_boot() in Drupal 5
     and runs on each page request, even for cached pages. hook_boot() now only runs
     for non-cached pages and thus can be used for code that was previously placed in
     hook_menu() with $may_cache = FALSE:

     Dynamic menu items under a '!$may_cache' block can often be simplified
     to remove references to arg(n) and use of '%<function-name>' to check
     conditions. See http://drupal.org/node/103114.

     The title and description arguments should not have strings wrapped in t(),
     because translation of these happen in a later stage in the menu system.
  */
  $may_cache = true;
  if ($may_cache) {
    $items['admin/content/video/customfields'] = array(
      'title' => 'Customfields',
      'description' => 'Administer video_customfields module settings',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'video_customfields_settings_form',
      ),
      'access arguments' => array(
        'administer site configuration',
      ),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Settings Hook
 *
 * @return
 *   string of form content or error message
 */
function video_customfields_settings_form() {

  //Must have "administer site configuration" and "administer video" privilages.
  if (!user_access('administer video')) {
    drupal_access_denied();
  }
  $form['customfields'] = array(
    '#type' => 'fieldset',
    '#weight' => -1,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Custom display fields'),
    '#description' => t('Creates custom fields. Fields only show up if you give them a name.'),
  );
  $form['customfields']['video_customfieldtitle'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field group title'),
    '#default_value' => variable_get('video_customfieldtitle', ''),
    '#description' => t('Title of the group of all custom fields.'),
  );
  $form['customfields']['video_customfield1'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 1 title'),
    '#default_value' => variable_get('video_customfield1', ''),
  );
  $form['customfields']['video_customfield2'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 2 title'),
    '#default_value' => variable_get('video_customfield2', ''),
  );
  $form['customfields']['video_customfield3'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 3 title'),
    '#default_value' => variable_get('video_customfield3', ''),
  );
  $form['customfields']['video_customfield4'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 4 title'),
    '#default_value' => variable_get('video_customfield4', ''),
  );
  $form['customfields']['video_customfield5'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 5 title'),
    '#default_value' => variable_get('video_customfield5', ''),
  );
  $form['customfields']['video_customfield6'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 6 title'),
    '#default_value' => variable_get('video_customfield6', ''),
  );
  $options = array(
    1 => 'Yes',
    0 => 'No',
  );
  $form['customfields']['video_customgroupcollapsed'] = array(
    '#type' => 'radios',
    '#title' => t('Start group initially collapsed'),
    '#options' => $options,
    '#default_value' => variable_get('video_customgroupcollapsed', 1),
    '#description' => t('Should the custom fields group be initially collapsed when creating and editing video nodes?'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter()
 * We use this to add some custom fields to the video creation form.
 * Fields will be displayed only if field title is set on settings page.
 */
function video_customfields_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'video_node_form' && isset($form['video']) && user_access('insert custom fields')) {

    //get node object from form
    $node = $form['#node'];
    $title1 = variable_get('video_customfield1', '');
    $title2 = variable_get('video_customfield2', '');
    $title3 = variable_get('video_customfield3', '');
    $title4 = variable_get('video_customfield4', '');
    $title5 = variable_get('video_customfield5', '');
    $title6 = variable_get('video_customfield6', '');

    //Only display the custom fields group if atleast one field has a title.
    if ($title1 . $title2 . $title3 . $title4 . $title5 . $title6 != '') {
      $form['customfields'] = array(
        '#type' => 'fieldset',
        '#title' => variable_get('video_customfieldtitle', 'Custom Fields'),
        '#collapsible' => TRUE,
        '#collapsed' => variable_get('video_customgroupcollapsed', FALSE),
        '#weight' => -17,
      );

      //If the custom field title is not blank, then display it.
      if ($title1 != '') {
        $form['customfields']['custom_field_1'] = array(
          '#type' => 'textfield',
          '#title' => $title1,
          '#maxlength' => 250,
          '#default_value' => $node->custom_field_1,
        );
      }
      if ($title2 != '') {
        $form['customfields']['custom_field_2'] = array(
          '#type' => 'textfield',
          '#title' => $title2,
          '#maxlength' => 250,
          '#default_value' => $node->custom_field_2,
        );
      }
      if ($title3 != '') {
        $form['customfields']['custom_field_3'] = array(
          '#type' => 'textfield',
          '#title' => $title3,
          '#maxlength' => 250,
          '#default_value' => $node->custom_field_3,
        );
      }
      if ($title4 != '') {
        $form['customfields']['custom_field_4'] = array(
          '#type' => 'textfield',
          '#title' => $title4,
          '#maxlength' => 250,
          '#default_value' => $node->custom_field_4,
        );
      }
      if ($title5 != '') {
        $form['customfields']['custom_field_5'] = array(
          '#type' => 'textarea',
          '#title' => $title5,
          '#rows' => 4,
          '#default_value' => $node->custom_field_5,
        );
      }
      if ($title6 != '') {
        $form['customfields']['custom_field_6'] = array(
          '#type' => 'textarea',
          '#title' => $title6,
          '#rows' => 4,
          '#default_value' => $node->custom_field_6,
        );
      }
    }
  }
}

/**
 * Implementation of hook_nodeapi()
 */
function video_customfields_nodeapi(&$node, $op, $teaser) {
  if ($node->type == 'video') {
    switch ($op) {
      case 'view':

        //If the main node view is being displayed then add the extra video information.
        if ($teaser == FALSE) {
          if ($node->custom_field_1 . $node->custom_field_2 . $node->custom_field_3 . $node->custom_field_4 . $node->custom_field_5 . $node->custom_field_6 != '') {

            //Make sure there is data to display.

            //Add the HTML formatted output of the custom fields to the bottom.
            $node->body .= theme('video_customfields', $node);
          }
        }
        break;
    }
  }
}

/**
 * Display custom fields on the view page.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_customfields($node) {

  //Adds the custom fields.
  $group_title = variable_get('video_customfieldtitle', '');

  //Title of the custom fields.
  $title1 = variable_get('video_customfield1', '');
  $title2 = variable_get('video_customfield2', '');
  $title3 = variable_get('video_customfield3', '');
  $title4 = variable_get('video_customfield4', '');
  $title5 = variable_get('video_customfield5', '');
  $title6 = variable_get('video_customfield6', '');

  //Run the fields through the input filter set for the node, then remove paragraphs.

  //Removes the <p> and </p> tags from the filter pass return. This allows each field to be on one line.

  //A better system might be to remove only the first and last <p></P> tags.
  $field1 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_1, $node->format, FALSE));
  $field2 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_2, $node->format, FALSE));
  $field3 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_3, $node->format, FALSE));
  $field4 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_4, $node->format, FALSE));
  $field5 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_5, $node->format, FALSE));
  $field6 = str_replace(array(
    '<p>',
    '</p>',
  ), '', check_markup($node->custom_field_6, $node->format, FALSE));
  $output = '';

  //Make sure all the titles are not blank, if not then display them.
  if ($title1 . $title2 . $title3 . $title4 . $title5 . $title6 != '') {
    $output = '<div class="videofields">';

    //Enclose all output in "videofields" div class.
    if ($group_title != '') {
      $output .= '<div class="title"><h2>' . check_plain($group_title) . '</h2></div>' . "\n";
    }
    if ($title1 != '' and $node->custom_field_1 != '') {
      $fields[] = array(
        'title' => $title1,
        'body' => $field1,
      );
    }
    if ($title2 != '' and $node->custom_field_2 != '') {
      $fields[] = array(
        'title' => $title2,
        'body' => $field2,
      );
    }
    if ($title3 != '' and $node->custom_field_3 != '') {
      $fields[] = array(
        'title' => $title3,
        'body' => $field3,
      );
    }
    if ($title4 != '' and $node->custom_field_4 != '') {
      $fields[] = array(
        'title' => $title4,
        'body' => $field4,
      );
    }
    if ($title5 != '' and $node->custom_field_5 != '') {
      $fields[] = array(
        'title' => $title5,
        'body' => $field5,
      );
    }
    if ($title6 != '' and $node->custom_field_6 != '') {
      $fields[] = array(
        'title' => $title6,
        'body' => $field6,
      );
    }
    $output .= theme('video_fields', $fields);

    //Generate all the fields HTML.
    $output .= '</div><br />';

    //Close the "videofields" class div.
  }
  return $output;
}

/**
 * Implementation of hook_theme().
 */
function video_customfields_theme() {
  return array(
    'video_customfields' => array(
      'arguments' => array(
        'node' => NULL,
      ),
    ),
  );
}

Functions

Namesort descending Description
theme_video_customfields Display custom fields on the view page.
video_customfields_form_alter Implementation of hook_form_alter() We use this to add some custom fields to the video creation form. Fields will be displayed only if field title is set on settings page.
video_customfields_help Implementation of hook_help().
video_customfields_menu Implementation of hook_menu().
video_customfields_nodeapi Implementation of hook_nodeapi()
video_customfields_perm Implementation of hook_perm().
video_customfields_settings_form Settings Hook
video_customfields_theme Implementation of hook_theme().