home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (nodewords_extra.module)

This example Drupal source code file (nodewords_extra.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, content, core, edit, empty, function, if, meta, options, php, tag, the, title

The nodewords_extra.module Drupal example source code

<?php
// $Id: nodewords_extra.module,v 1.1.2.43 2009/12/28 21:04:49 kiam Exp $

/**
 * @file
 * Allows users to add extra meta tags to Drupal pages.
 */

/**
 * @addtogroup nodewords_extra
 * @{
 */

/**
 * Implements hook_form_FORM_ID_alter().
 */
function nodewords_extra_form_nodewords_settings_form_alter(&$form, &$form_state) {
  $form['metatags_creation']['nodewords_add_dc_schema'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add the Dublin Core schema meta tag'),
    '#default_value' => variable_get('nodewords_add_dc_schema', FALSE),
  );
}

/**
 * Implements hook_menu().
 */
function nodewords_extra_menu() {
  $items = array();

  $items['labels.rdf'] = array(
    'title' => 'ICRA labels',
    'page callback' => 'nodewords_extra_verification_content',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_nodewords_api().
 */
function nodewords_extra_nodewords_api() {
  return array(
    'version' => '1.9',
  );
}

/**
 * Implements hook_nodewords_tags_info().
 */
function nodewords_extra_nodewords_tags_info() {
  $tags = array(
    'dc.contributor' => array(
      'tag:context:allowed' => array(
        NODEWORDS_TYPE_DEFAULT,
        NODEWORDS_TYPE_NODE,
        NODEWORDS_TYPE_PAGE,
      ),
      'tag:function:prefix' => 'nodewords_extra_dc_contributor',
      'tag:template' => array('dc.contributor' => NODEWORDS_META),
      'widget:label' => t('Dublin Core contributor'),
      'widget:permission' => 'edit meta tag Dublin Core CONTRIBUTOR',
    ),
    'dc.creator' => array(
      'tag:context:allowed' => array(
        NODEWORDS_TYPE_DEFAULT,
        NODEWORDS_TYPE_NODE,
        NODEWORDS_TYPE_PAGE,
      ),
      'tag:function:prefix' => 'nodewords_extra_dc_creator',
      'tag:template' => array('dc.creator' => NODEWORDS_META),
      'widget:label' => t('Dublin Core creator'),
    ),
    'dc.date' => array(
      'tag:context:allowed' => array(
        NODEWORDS_TYPE_NODE,
        NODEWORDS_TYPE_PAGE,
      ),
      'tag:function:prefix' => 'nodewords_extra_dc_date',
      'tag:template' => array('dc.date' => NODEWORDS_META),
      'widget:label' => t('Dublin Core date'),
      'widget:permission' => 'edit meta tag Dublin Core DATE',
    ),
    'dc.description' => array(
      'tag:function:prefix' => 'nodewords_extra_dc_description',
      'tag:template' => array('dc.description' => NODEWORDS_META),
      'widget:label' => t('Dublin Core description'),
      'widget:permission' => 'edit meta tag Dublin Core DESCRIPTION',
    ),
    'dc.publisher' => array(
      'tag:context:allowed' => array(
        NODEWORDS_TYPE_DEFAULT,
        NODEWORDS_TYPE_PAGE,
      ),
      'tag:function:prefix' => 'nodewords_extra_dc_publisher',
      'tag:template' => array('dc.publisher' => NODEWORDS_META),
      'widget:label' => t('Dublin Core publisher'),
      'widget:permission' => 'edit meta tag Dublin Core PUBLISHER',
    ),
    'dc.title' => array(
      'tag:context:denied' => array(NODEWORDS_TYPE_DEFAULT),
      'tag:function:prefix' => 'nodewords_extra_dc_title',
      'tag:template' => array('dc.title' => NODEWORDS_META),
      'widget:label' => t('Dublin Core title'),
      'widget:permission' => 'edit meta tag Dublin Core TITLE',
    ),
    'location' => array(
      'tag:function:prefix' => 'nodewords_extra_location',
      'tag:template' => array(
        'geo.position' => NODEWORDS_META,
        'icbm' => NODEWORDS_META,
      ),
      'widget:label' => t('Location'),
      'widget:permission' => 'edit location meta tags',
    ),
    'pics-label' => array(
      'tag:context:allowed' => array(
        NODEWORDS_TYPE_DEFAULT,
        NODEWORDS_TYPE_NODE,
        NODEWORDS_TYPE_PAGE,
      ),
      'tag:attributes' => array(
        'meta' => array(
          'title' => 'ICRA labels',
          'type' => 'application/rdf+xml',
        ),
      ),
      'tag:function:prefix' => 'nodewords_extra_pics',
      'tag:template' => array(
        'pics-label' => NODEWORDS_HTTP_EQUIV,
        'meta' => NODEWORDS_LINK_REL,
      ),
      'tag:weight' => array('meta' => -10),
      'widget:label' => t('PICS labels'),
      'widget:permission' => 'edit meta tag PICS-LABEL',
    ),
  );

  return $tags;
}

function nodewords_extra_nodewords_tags_output_alter(&$output, $parameters) {
  $bool = (
    variable_get('nodewords_add_dc_schema', FALSE) &&
    isset($parameters['output']) &&
    $parameters['type'] != NODEWORDS_TYPE_NONE &&
    $parameters['output'] == 'head'
  );

  if ($bool) {
    $output = (
      '<link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" />' . "\n" .
      $output
    );
  }
}

/**
 * Implements hook_perm().
 */
function nodewords_extra_perm() {
  return array(
    'edit meta tag Dublin Core CONTRIBUTOR',
    'edit meta tag Dublin Core CREATOR',
    'edit meta tag Dublin Core DATE',
    'edit meta tag Dublin Core DESCRIPTION',
    'edit meta tag Dublin Core PUBLISHER',
    'edit meta tag Dublin Core TITLE',
    'edit location meta tags',
    'edit meta tag PICS-LABEL',
  );
}

function nodewords_extra_verification_content() {
  drupal_set_header('Content-Type: application/rdf+xml; charset=utf-8');

  print variable_get('nodewords_icra_validation_content', '');
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_contributor_form(&$form, $content, $options) {
  $form['dc.contributor'] = array(
    '#tree' => TRUE,
  );

  $form['dc.contributor']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Dublin Core contributor'),
    '#description' => t('Enter the name of an entity responsible for making contributions to the resource. Examples of a contributor include a person, an organization, or a service.'),
    '#default_value' => empty($content['value']) ? (!empty($options['default']['dc.contributor']['value']) ? $options['default']['dc.contributor']['value'] : '') : $content['value'],
    '#size' => 60,
    '#maxlength' => variable_get('nodewords_max_size', 350),
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_contributor_prepare(&$tags, $content, $options) {
  if (!empty($content['value'])) {
    $tags['dc.contributor'] = $content['value'];
  }
  elseif (!empty($options['default']['dc.contributor']['value'])) {
    $tags['dc.contributor'] = $options['default']['dc.contributor']['value'];
  }
  else {
    if ($options['type'] == NODEWORDS_TYPE_NODE && count($options['ids']) == 1) {
      $bool = (
        ($node = node_load($options['ids'][0])) &&
        ($uid = $node->uid) &&
        ($user = user_load($uid)) &&
        $user->name
      );

      if ($bool) {
        $tags['dc.contributor'] = $user->name;
      }
    }
  }
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_creator_form(&$form, $content, $options) {
  $form['dc.creator'] = array(
    '#tree' => TRUE,
  );

  $form['dc.creator']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Dublin Core creator'),
    '#description' => t('Enter the name of an entity primarily responsible for making the resource. Examples of a creator include a person, an organization, or a service.'),
    '#default_value' => empty($content['value']) ? (!empty($options['default']['dc.creator']['value']) ? $options['default']['dc.creator']['value'] : '') : $content['value'],
    '#size' => 60,
    '#maxlength' => variable_get('nodewords_max_size', 350),
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_creator_prepare(&$tags, $content, $options) {
  if (!empty($content['value'])) {
    $tags['dc.creator'] = $content['value'];
  }
  elseif (!empty($options['default']['dc.creator']['value'])) {
    $tags['dc.creator'] = $options['default']['dc.creator']['value'];
  }
  else {
    if ($options['type'] == NODEWORDS_TYPE_NODE && count($options['ids']) == 1) {
      $bool = (
        ($node = node_load($options['ids'][0])) &&
        ($uid = $node->uid) &&
        ($user = user_load($uid)) &&
        $user->name
      );

      if ($bool) {
        $tags['dc.creator'] = $user->name;
      }
    }
  }
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_date_form(&$form, $content, $options) {
  $form['dc.date'] = array(
    '#tree' => TRUE,
  );

  $form['dc.date']['value'] = array(
    '#type' => 'date',
    '#title' => t('Dublin Core date'),
    '#description' => t('A point or period of time associated with an event in the lifecycle of the resource. The date should be relative to UTC.'),
    '#default_value' => empty($content['value']) ? array() : $content['value'],
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_date_prepare(&$tags, $content, $options) {
  if (!empty($content['value'])) {
    $time = mktime(0, 0, 0, $content['value']['month'], $content['value']['day'], $content['value']['year']);
    if ($time > 0) {
      $tags['dc.date'] = date('Y-m-d\TH:i:s\Z', $time);
    }
  }
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_description_form(&$form, $content, $options) {
  if (!isset($form['description'])) {
    $bool = (
      isset($options['node_type']) &&
      variable_get('nodewords_use_teaser_' . $options['node_type'], variable_get('nodewords_use_teaser', FALSE))
    );

    if (!$bool) {
      $form['dc.description'] = array(
        '#tree' => TRUE,
      );

      $form['dc.description']['value'] = array(
        '#type' => 'textarea',
        '#title' => t('Description'),
        '#description' => t('Enter a description. Limit your description to about 20 words, with a maximum of %count characters. It should not contain any HTML tags or other formatting.', array('%count' => variable_get('nodewords_max_size', 350))),
        '#default_value' => empty($content['value']) ? (!empty($options['default']['dc.description']['value']) ? $options['default']['dc.description']['value'] : '') : $content['value'],
        '#cols' => 60,
        '#rows' => 6,
      );
    }
  }
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_description_prepare(&$tags, $content, $options) {
  if (empty($tags['description'])) {
    if (empty($content['value'])) {
      if (!empty($options['default']['dc.description']['value'])) {
        $value = $options['default']['dc.description']['value'];
      }
      elseif (!empty($options['default']['description']['value'])) {
        $value = $options['default']['description']['value'];
      }
      else {
        $value = '';
      }
    }
    else {
      $value = $content['value'];
    }

    $tags['dc.description'] = $value;
  }
  else {
    $tags['dc.description'] = $tags['description'];
  }
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_publisher_form(&$form, $content, $options) {
  $form['dc.publisher'] = array(
    '#tree' => TRUE,
  );

  $form['dc.publisher']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Dublin Core publisher'),
    '#description' => t('Enter a name of an entity responsible for making the resource available. Examples of a publisher include a person, an organization, or a service.'),
    '#default_value' => empty($options['default']['dc.publisher']['value']) ? '' : $options['default']['dc.publisher']['value'],
    '#size' => 60,
    '#maxlength' => variable_get('nodewords_max_size', 350),
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_publisher_prepare(&$tags, $content, $options) {
  $tags['dc.publisher'] = empty($options['default']['dc.publisher']['value']) ? '' : $options['default']['dc.publisher']['value'];
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_dc_title_form(&$form, $content, $options) {
  $form['dc.title'] = array(
    '#tree' => TRUE,
  );

  $form['dc.title']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Dublin Core title'),
    '#description' => t('Enter an alternative title. Do not use the value already used for the HTML tag TITLE, or you will probably get warning reports about duplicated titles from the search engines.'),
    '#default_value' => empty($content['value']) ? '' : $content['value'],
    '#size' => 60,
    '#maxlength' => variable_get('nodewords_max_size', 350),
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_dc_title_prepare(&$tags, $content, $options) {
  if (empty($content['value'])) {
    if ($options['type'] == NODEWORDS_TYPE_NODE && count($options['ids']) == 1) {
      if ($node = node_load($options['ids'][0])) {
        $content['value'] = $node->title;
      }
    }
  }

  $tags['dc.title'] = empty($content['value']) ? '' : $content['value'];
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_location_form(&$form, $content, $options) {
  $form['location'] = array(
    '#tree' => TRUE,
  );

  $form['location']['latitude'] = array(
    '#type' => 'textfield',
    '#title' => t('Location coordinates (latitude, longitude)'),
    '#default_value' => empty($content['latitude']) ? (!empty($options['default']['location']['latitude']) ? $options['default']['location']['latitude'] : '') : $content['latitude'],
    '#element_validate' => array('nodewords_extra_location_form_validate'),
    '#size' => 14,
    '#maxlength' => 14,
    '#prefix' => '<div class="container-inline">',
    '#latitude' => TRUE,
  );

  $form['location']['longitude'] = array(
    '#type' => 'textfield',
    '#title' => '',
    '#default_value' => empty($content['longitude']) ? (!empty($options['default']['location']['longitude']) ? $options['default']['location']['longitude'] : '') : $content['longitude'],
    '#size' => 14,
    '#element_validate' => array('nodewords_extra_location_form_validate'),
    '#maxlength' => 14,
    '#suffix' => '</div>',
  );
}

/**
 * Validate the values passed as latitude, and longitude.
 */
function nodewords_extra_location_form_validate($element, &$form_state) {
  if (isset($element['#latitude'])) {
    if (!empty($element['#value']) && (!is_numeric($element['#value']) || $element['#value'] < -90.0 || $element['#value'] > 90.0)) {
      form_error($element, t('Latitude must be a number between -90.0 and 90.0 (extrems included).'));
    }
  }
  else {
    if (!empty($element['#value']) && (!is_numeric($element['#value']) || $element['#value'] < -180.0 || $element['#value'] > 180.0)) {
      form_error($element, t('Longitude must be a number between -180.0 and 180.0 (extrems included).'));
    }
  }
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_location_prepare(&$tags, $content, $options) {
  if (empty($content['latitude']) || empty($content['longitude'])) {
    $content['latitude'] = !empty($options['default']['location']['latitude']) ? $options['default']['location']['latitude'] : '';
    $content['longitude'] = !empty($options['default']['location']['longitude']) ? $options['default']['location']['longitude'] : '';
  }

  if (!empty($content['latitude']) && !empty($content['longitude'])) {
    $tags['location:geo.position'] = $content['latitude'] . ';' . $content['longitude'];
    $tags['location:icbm'] = $content['latitude'] . ',' . $content['longitude'];
  }

  $bool = (
    empty($tags['location:geo.position']) &&
    $options['type'] == NODEWORDS_TYPE_NODE &&
    count($options['ids']) == 1 &&
    module_exists('location') &&
    ($node = node_load($options['ids'][0]))
  );

  if ($bool && isset($node->locations[0]['latitude']) && isset($node->locations[0]['longitude'])) {
    $tags['location:geo.position'] = $node->locations[0]['latitude'] . ';' . $node->locations[0]['longitude'];
    $tags['location:icbm'] = $node->locations[0]['latitude'] . ',' . $node->locations[0]['longitude'];
  }
}

/**
 * Set the form fields used to implement the options for the meta tag.
 */
function nodewords_extra_pics_form(&$form, $content, $options) {
  $form['pics-label'] = array(
    '#tree' => TRUE,
  );

  $form['pics-label']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('PICS labels'),
    '#description' => t('Enter the ICRA PICS labels only, without any extra characters; ICRA PICS labels should be something similar to %icra-pics-example. You can use the online <a href="@icra-label-generator">ICRA label generator</a>.', array('%icra-pics-example' => 'n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 1', '@icra-label-generator' => 'http://www.icra.org/label/generator/')),
    '#default_value' => empty($content['value']) ? (!empty($options['default']['pics-label']['value']) ? $options['default']['pics-label']['value'] : '') : $content['value'],
    '#size' => 60,
    '#maxlength' => variable_get('nodewords_max_size', 350),
  );
}

/**
 * Set the meta tag content.
 */
function nodewords_extra_pics_prepare(&$tags, $content, $options) {
  if (empty($content['value'])) {
    $content['value'] = !empty($options['default']['pics-label']['value']) ? $options['default']['pics-label']['value'] : '';
  }

  if (!empty($content['value'])) {
    if ($options['type'] == NODEWORDS_TYPE_NODE && count($options['ids']) == 1) {
      $tags['pics-label'] = strtr(
        '(pics-1.1 "http://www.icra.org/pics/vocabularyv03/" l gen true for %url r (%pics))',
        array(
          '%url' => url("node/{$options['ids'][0]}", array('absolute' => TRUE)),
          '%pics' => $content['value'],
        )
      );
    }
  }

  if (variable_get('nodewords_icra_validation_content', '')) {
    $tags['pics-label:meta'] = url('labels.rdf', array('absolute' => TRUE));
  }
}

function nodewords_extra_pics_settings_form(&$form, $form_id, $options) {
  if ($form_id == 'nodewords_settings_form') {
    $form['metatags_creation']['nodewords_icra_validation_content'] = array(
      '#type' => 'textarea',
      '#title' => t('ICRA validation content'),
      '#description' => t('Enter the validation content ICRA sent you. Leave this field empty if you do not know what a ICRA validation file is. For more information, see <a href="@irca-url">The Family Online Safety Institute: Protecting your online future</a>.', array('@irca-url' => 'http://www.icra.org/support/')),
      '#default_value' => variable_get('nodewords_icra_validation_content', ''),
      '#size' => 60,
    );
  }
}

/**
 * @} End of "addtogroup nodewords_extra".
 */

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal nodewords_extra.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.