You are here

iframe.module in Iframe 7

Same filename and directory in other branches
  1. 8.2 iframe.module
  2. 8 iframe.module
  3. 6 iframe.module

Defines an iframe field with all attributes.

Modul-programming see also http://de.slideshare.net/zugec/fields-in-core-how-to-create-a-custom-field

File

iframe.module
View source
<?php

/**
 * @file
 * Defines an iframe field with all attributes.
 *
 * Modul-programming
 *    see also http://de.slideshare.net/zugec/fields-in-core-how-to-create-a-custom-field
 */
define('iframe_EXTERNAL', 'external');
define('iframe_INTERNAL', 'internal');
define('iframe_FRONT', 'front');
define('iframe_EMAIL', 'email');
define('iframe_DOMAINS', 'aero|arpa|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
define('DEBUG_LEVEL', 0);
if (!defined('DEBUG_LEVEL')) {
  define('DEBUG_LEVEL', 4);
}

/* function "dpr" is from devel-module, we will use it if exists */
function iframe_debug($level, $func = 'iframe', $debugvar = NULL) {
  if ($level <= DEBUG_LEVEL) {
    $debugtitle = '[iframe(' . $level . ')] ' . $func;
    if (!function_exists('kpr') && is_readable('../devel/devel.module')) {
      include_once '../devel/devel.module';
    }
    if (function_exists('kpr')) {
      kpr(array(
        $debugtitle,
        $debugvar,
      ), NULL, $debugtitle);
    }
    else {
      drupal_set_message($debugtitle . ' ' . print_r($debugvar, TRUE), 'warning');
    }
  }
}

/**
 * Implements hook_help().
 */
function iframe_help($path, $arg) {
  switch ($path) {
    case 'admin/help#iframe':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Iframe module defines an iframe field type for the Field module. Further definable are attributes for styling the iframe, like: URL, width, height, title, class, frameborder, scrolling and transparency. See the <a href="@field-help">Field module help page</a> for more information about fields.', array(
        '@field-help' => url('admin/help/field'),
      )) . '</p>';
      return $output;
  }
}

/***************************************************************
 * Field Type API hooks
 ***************************************************************/

/**
 * Implements hook_field_info().
 * Shows the "Field type" while adding a new field to the content-type
 * (Step 1)
 */
function iframe_field_info() {
  return array(
    'iframe' => array(
      'label' => t('Iframe'),
      'description' => t('This field stores the parameters for an iframe.'),
      /* define defaults either in 'settings' (globally) OR in 'instance_settings' (per content-type) */

      // settings - used as default values for the general field definition
      'settings' => array(
        #'title' => '',

        #'url' => '',

        #'width' => '',

        #'height' => '',
        'class' => 'field-iframe',
        /* class-name is the only attribute, which we use additive */
        'frameborder' => 0,
        /* "0" or "yes" */
        'scrolling' => 'auto',
        'transparency' => 0,
        'tokensupport' => 0,
      ),
      'instance_settings' => array(
        #'title' => '',

        #'url' => '',
        'width' => '100%',
        'height' => '800',
        'class' => 'field-iframe-instance',
        #'frameborder' => 0, /* "0" or "yes" */

        #'scrolling' => 'auto',

        #'transparency' => 0,

        #'tokensupport' => 0,
        'expose_class' => 1,
        'allowfullscreen' => 0,
      ),
      'default_widget' => 'iframe_widget_url',
      'default_formatter' => 'iframe_formatter_default',
    ),
  );
}

/**
 * Implements hook_field_settings_form().
 * These settings apply to the iframe field everywhere(!) it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.
 * Field XYZ sub-menu "Field settings"
 * (Step 2)
 */
function iframe_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];

  // as defined in iframe_field_info()
  $form = array();
  if ($field['type'] == 'iframe') {
    $form['class'] = array(
      '#type' => 'textfield',
      '#title' => t('Additional CSS Class'),
      '#default_value' => !empty($settings['class']) ? $settings['class'] : '',
      '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
    );
    $form['frameborder'] = array(
      '#type' => 'select',
      '#title' => t('Frameborder'),
      '#options' => array(
        '0' => t('No frameborder'),
        '1' => t('Show frameborder'),
      ),
      '#default_value' => !empty($settings['frameborder']) ? $settings['frameborder'] : 0,
      '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0 = no.'),
    );
    $form['scrolling'] = array(
      '#type' => 'select',
      '#title' => t('Scrolling'),
      '#options' => array(
        'auto' => t('Scrolling automatic'),
        'no' => t('Scrolling disabled'),
        'yes' => t('Scrolling enabled'),
      ),
      '#default_value' => !empty($settings['scrolling']) ? $settings['scrolling'] : 'auto',
      '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
    );
    $form['transparency'] = array(
      '#type' => 'select',
      '#title' => t('Transparency'),
      '#options' => array(
        '0' => t('No transparency'),
        '1' => t('Allow transparency'),
      ),
      '#default_value' => !empty($settings['transparency']) ? $settings['transparency'] : '0',
      '#description' => t('Allow transparency per CSS in the outer iframe tag. You have to set background-color:transparent in your iframe body tag too!'),
    );
    $form['tokensupport'] = array(
      '#type' => 'select',
      '#title' => t('Token Support'),
      '#options' => array(
        '0' => t('No tokens allowed'),
        '1' => t('Tokens only in title field'),
        '2' => t('Tokens for title and url field'),
      ),
      '#default_value' => !empty($settings['tokensupport']) ? $settings['tokensupport'] : '0',
      '#description' => t('Are tokens allowed for users to use in title or URL field?'),
    );
    if (!module_exists('token')) {
      $form['tokensupport']['#description'] .= ' ' . t('Attention: token module is not enabled currently!');
    }
  }
  return $form;
}

/**
 * Implements hook_field_instance_settings_form().
 * These settings apply only to the iframe field when used in this special content-type.
 * Field XYZ sub-menu "Edit"
 * (Step 3)
 */
function iframe_field_instance_settings_form($field, $instance) {
  $field_settings = $field['settings'];

  // as defined before in iframe_field_settings_form() ! has preference over instance_settings
  $settings = $instance['settings'];

  // default instance_settings from iframe_field_info
  $form = array();
  if ($field['type'] == 'iframe') {
    $form['width'] = array(
      '#type' => 'textfield',
      '#title' => t('Iframe width'),
      '#default_value' => !empty($settings['width']) ? $settings['width'] : (!empty($field_settings['width']) ? $field_settings['width'] : ''),
      '#description' => t('Iframes need a fixed width and height; only numbers are allowed, or percentage values. (e.g. 500px => "500", 80% => "80%")'),
      //'#disabled' => $has_data,
      '#maxlength' => 4,
      '#size' => 4,
    );
    $form['height'] = array(
      '#type' => 'textfield',
      '#title' => t('Iframe height'),
      '#default_value' => !empty($settings['height']) ? $settings['height'] : (!empty($field_settings['height']) ? $field_settings['height'] : ''),
      '#description' => t('Iframes need a fixed width and height; only numbers are allowed, or percentage values. (e.g. 500px => "500", 80% => "80%")'),
      '#maxlength' => 4,
      '#size' => 4,
    );
    $form['class'] = array(
      '#type' => 'textfield',
      '#title' => t('Additional CSS Class'),
      '#default_value' => !empty($settings['class']) ? $settings['class'] : (!empty($field_settings['class']) ? $field_settings['class'] : ''),
      '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
    );
    $form['expose_class'] = array(
      '#type' => 'checkbox',
      '#title' => t('Expose Additional CSS Class'),
      '#default_value' => isset($settings['expose_class']) ? (int) $settings['expose_class'] : 1,
      '#description' => t('Allow author to specify an additional class attribute for this iframe.'),
    );
    $form['frameborder'] = array(
      '#type' => 'select',
      '#title' => t('Frameborder'),
      '#options' => array(
        '0' => t('No frameborder'),
        '1' => t('Show frameborder'),
      ),
      '#default_value' => !empty($settings['frameborder']) ? $settings['frameborder'] : (!empty($field_settings['frameborder']) ? $field_settings['frameborder'] : 0),
      '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0 = no.'),
    );
    $form['scrolling'] = array(
      '#type' => 'select',
      '#title' => t('Scrolling'),
      '#options' => array(
        'auto' => t('Scrolling automatic'),
        'no' => t('Scrolling disabled'),
        'yes' => t('Scrolling enabled'),
      ),
      '#default_value' => !empty($settings['scrolling']) ? $settings['scrolling'] : (!empty($field_settings['scrolling']) ? $field_settings['scrolling'] : 'auto'),
      '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
    );
    $form['transparency'] = array(
      '#type' => 'select',
      '#title' => t('Transparency'),
      '#options' => array(
        '0' => t('No transparency'),
        '1' => t('Allow transparency'),
      ),
      '#default_value' => !empty($settings['transparency']) ? $settings['transparency'] : (!empty($field_settings['transparency']) ? $field_settings['transparency'] : 0),
      '#description' => t('Allow transparency per CSS in the outer iframe tag. You have to set background-color:transparent in your iframe body tag too!'),
    );
    $form['tokensupport'] = array(
      '#type' => 'select',
      '#title' => t('Token Support'),
      '#options' => array(
        '0' => t('no tokens allowed'),
        '1' => t('tokens only in title field'),
        '2' => t('tokens for title and url field'),
      ),
      '#default_value' => !empty($settings['tokensupport']) ? $settings['tokensupport'] : (!empty($field_settings['tokensupport']) ? $field_settings['tokensupport'] : '0'),
      '#description' => t('Are tokens allowed for users to use in title or URL field?'),
    );
    if (!module_exists('token')) {
      $form['tokensupport']['#description'] .= ' ' . t('Attention: token module is not enabled currently!');
    }
    $form['allowfullscreen'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow fullscreen'),
      '#default_value' => isset($settings['allowfullscreen']) ? (int) $settings['allowfullscreen'] : 0,
      '#description' => t('Allow author to specify an additional class attribute for this iframe.'),
    );
  }
  return $form;
}

/**
 * Implements hook_field_validate().
 *
 * Possible error codes:
 * - 'widthheight': The value should be between 1 and 9999, or between 1% and 100%
 *
 * (entity_type: "node", entity: node-object, field: (field-settings + field-inst-settings) ..., items: current-items-of-page)
 */
function iframe_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if (isset($item['url']) && !empty($item['url'])) {
      if (isset($item['width']) && (empty($item['width']) || (int) $item['width'] < 1)) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => '_iframe_widthsize_invalid',
          'message' => t('%name: the value of width may be no less than 1.', array(
            '%name' => t($instance['label']),
          )),
        );
      }
      if (isset($item['height']) && (empty($item['height']) || $item['height'] < 1)) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => '_iframe_heightsize_invalid',
          'message' => t('%name: the value of height may be no less than 1.', array(
            '%name' => t($instance['label']),
          )),
        );
      }
    }
  }
  iframe_debug(1, 'iframe_field_validate errors', $errors);
}

/**
 * Implements hook_field_presave().
 *
 * We have to populate all missing default-values which were defined, but not edited by the current author
 * ($entity can be manipulated, because it shows only to the object)
 * (entity_type: "node", entity: node-object, field: (field-settings + field-inst-settings) ..., items: current-items-of-page)
 */
function iframe_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $settings = $instance['settings'];
  $settings += $field['settings'];
  if ($field['type'] == 'iframe') {
    foreach ($items as $delta => $item) {
      if (isset($item['url'])) {

        // fill in missing attributes for saving
        $items[$delta] = $items[$delta] + $settings;
      }
    }
    iframe_debug(2, 'iframe_field_presave', array(
      'entity_type' => $entity_type,
      'entity' => $entity,
      'field' => $field,
      'instance' => $instance,
      'items' => $items,
    ));
  }
}
function iframe_field_attach_presave($entity_type, $entity) {
  iframe_debug(2, 'iframe_field_attach_presave', array(
    'entity_type' => $entity_type,
    'entity' => $entity,
  ));
}

/**
 * Implements hook_field_is_empty().
 */
function iframe_field_is_empty($item, $field) {
  if ('iframe' == $field['type']) {
    if (empty($item['url'])) {
      return TRUE;
    }
  }
  return FALSE;
}

/***********************************************************************
 *  Field Type API: Formatter
 **********************************************************************/

/**
 * Implements hook_field_formatter_info().
 * used in field "manage display" for formatters
 * (Step 4)
 */
function iframe_field_formatter_info() {
  return array(
    'iframe_formatter_default' => array(
      'label' => t('Title, over iframe (default)'),
      'field types' => array(
        'iframe',
      ),
    ),
    'iframe_formatter_only' => array(
      'label' => t('Iframe without title'),
      'field types' => array(
        'iframe',
      ),
    ),
    'iframe_formatter_asurl' => array(
      'label' => t('A link with the given title'),
      'field types' => array(
        'iframe',
      ),
    ),
    'iframe_formatter_asurlwithuri' => array(
      'label' => t('A link with the uri as title'),
      'field types' => array(
        'iframe',
      ),
    ),
    'iframe_formatter_hidden' => array(
      'label' => t('hidden - not shown (migration only)'),
      'field types' => array(
        'iframe',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 * field settings = settings from "Feldtyp = field type" for the given generic field in the content-type
 * instance settings = generic settings from iframe-field (specific only in this content-type)
 * ==> take (1) field-settings => overwrite (if set) with (2) instance-settings => overwrite with (3) item-settings
 */
function iframe_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {

  // object_type = node, object = Article-node-object
  iframe_debug(2, 'iframe_field_formatter_view', array(
    'field' => $field,
    'instance' => $instance,
    'langcode' => $langcode,
    'items' => '...',
    'display' => $display,
  ));
  $element = array();
  $settings = $instance['settings'] + $field['settings'];

  // f-s rules i-s
  if (isset($settings['user_register_form'])) {
    unset($settings['user_register_form']);
  }

  # merge for sub-fields: "class"
  $settings['class'] = @trim($field['settings']['class'] . " " . $instance['settings']['class']);
  $class = $settings['class'];
  foreach ($items as $delta => $item) {
    $items[$delta]['delta'] = $delta;
    $items[$delta]['id'] = drupal_html_id($field['type'] . '-' . $field['field_name'] . '-' . $delta);
    $iclass = isset($item['class']) ? $item['class'] : '';
    if (isset($settings['expose_class']) && $settings['expose_class'] > 0) {
      $items[$delta]['class'] = trim($class . " " . $iclass);
    }
    else {
      $items[$delta]['class'] = trim($class);
    }
  }
  if ($display['type'] == 'iframe_formatter_default') {
    foreach ($items as $delta => $item) {
      $element[$delta]['#markup'] = iframe_formatter_default_helper($item, $delta);
    }
  }
  elseif ($display['type'] == 'iframe_formatter_only') {
    foreach ($items as $delta => $item) {
      $element[$delta]['#markup'] = iframe_formatter_only_helper($item, $delta);
    }
  }
  elseif ($display['type'] == 'iframe_formatter_asurl') {
    foreach ($items as $delta => $item) {
      $element[$delta]['#markup'] = iframe_formatter_asurl_helper($item, $delta);
    }
  }
  elseif ($display['type'] == 'iframe_formatter_asurlwithuri') {
    foreach ($items as $delta => $item) {
      $element[$delta]['#markup'] = iframe_formatter_asurlwithuri_helper($item, $delta);
    }
  }
  elseif ($display['type'] == 'iframe_formatter_hidden') {
    foreach ($items as $delta => $item) {
      $element[$delta]['#markup'] = '';
    }
  }
  return $element;
}

/**
 * Formatter function for 'default' text field formatter.
 */
function iframe_formatter_default_helper($item, $delta) {

  // If no url given display nothing.
  if (empty($item['url'])) {
    return '';
  }
  if (!isset($item['title'])) {
    $item['title'] = '';
  }

  // Display all
  return iframe_iframe($item['title'], $item['url'], $item);
}

/**
 * Formatter function for 'iframeonly' text field formatter.
 */
function iframe_formatter_only_helper($item, $delta) {

  // If no url given display nothing.
  if (empty($item['url'])) {
    return '';
  }

  // Display all
  return iframe_iframe('', $item['url'], $item);
}

/**
 * Formatter function for 'asurl' text field formatter.
 */
function iframe_formatter_asurl_helper($item, $delta) {

  // If no url given display nothing.
  if (empty($item['url'])) {
    return '';
  }

  // Display all
  $linktext = empty($item['title']) ? $item['url'] : $item['title'];
  return l($linktext, $item['url'], $item);
}

/**
 * Formatter function for 'asurlwithuri' text field formatter.
 */
function iframe_formatter_asurlwithuri_helper($item, $delta) {

  // If no url given display nothing.
  if (empty($item['url'])) {
    return '';
  }

  // Display all
  $linktext = $item['url'];
  return l($linktext, $item['url'], $item);
}

/**
 * Formatter function for 'hidden' text field formatter.
 */
function iframe_formatter_hidden_helper($item, $delta) {
  return '';
}

/*
 * like central function
 * form the iframe code
 */
function iframe_iframe($text, $path, $options = FALSE) {
  global $user, $language, $node;
  iframe_debug(1, 'iframe_iframe item', $options);
  if (!$options) {
    $options = array();
  }

  // Merge in defaults.
  $options += array(
    'html' => FALSE,
    'style' => '',
  );
  $allow = array();
  if (!isset($options['width'])) {
    $options['width'] = '100%';
  }
  if (!isset($options['height'])) {
    $options['height'] = '701';
  }
  if (isset($options['frameborder'])) {
    if ($options['frameborder'] > 0) {
      $options['style'] .= 'border-width:2px;';
    }
    else {
      $options['style'] .= 'border-width:0;';
    }
    unset($options['frameborder']);
  }
  if (isset($options['scrolling'])) {
    if ($options['scrolling'] == 'yes') {
      $options['style'] .= 'overflow:scroll;';
    }
    elseif ($options['scrolling'] == 'no') {
      $options['style'] .= 'overflow:hidden;';
    }
    else {

      # auto
      $options['style'] .= 'overflow:auto;';
    }
    unset($options['scrolling']);
  }
  if (isset($options['transparency'])) {
    if ($options['transparency'] > 0) {
      $options['style'] .= 'background-color:transparent;';
    }
    unset($options['transparency']);
  }
  if (!isset($options['tokensupport']) || $options['tokensupport'] < 0) {
    $options['tokensupport'] = 0;
  }
  if (isset($options['allowfullscreen']) && $options['allowfullscreen'] == 1) {
    $allow[] = 'fullscreen';
  }
  unset($options['allowfullscreen']);
  $allow[] = 'autoplay';
  $allow[] = 'camera';
  $allow[] = 'microphone';
  $allow[] = 'payment';
  $allow[] = 'accelerometer';
  $allow[] = 'geolocation';
  $allow[] = 'encrypted-media';
  $allow[] = 'gyroscope';
  $options['allow'] = implode(';', $allow);

  // Append active class.
  if ($path == $_GET['q'] || $path == '<front>' && drupal_is_front_page()) {
    if (isset($options['class'])) {
      $options['class'] .= ' active';
    }
    else {
      $options['class'] = 'active';
    }
  }

  // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
  // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
  if (isset($options['title']) && strpos($options['title'], '<') !== FALSE) {
    $options['title'] = strip_tags($options['title']);
  }
  $options_link = array();
  $options_link['attributes'] = array();
  $options_link['attributes']['title'] = $options['title'];
  if (module_exists('token')) {

    // Token Support for field "url" and "title"
    if (!isset($node)) {
      $node = menu_get_object();
    }
    $tokensupport = !empty($options['tokensupport']) ? (int) $options['tokensupport'] : 0;
    if ($tokensupport > 0) {
      $text = token_replace($text, array(
        'user' => $user,
        'node' => $node,
      ), array(
        'language' => $language,
      ));

      // node:, user: are default token targets
    }
    if ($tokensupport > 1) {
      $path = token_replace($path, array(
        'user' => $user,
        'node' => $node,
      ), array(
        'language' => $language,
      ));
    }
  }
  if (!isset($options['name']) && !empty($options['id'])) {
    $options['name'] = $options['id'];
  }
  $iframearguments_allowed = array(
    'title',
    'width',
    'height',
    'class',
    'style',
    'id',
    'name',
    'allow',
  );
  $iframearguments = array();
  foreach ($iframearguments_allowed as $argkey) {
    if (isset($options[$argkey])) {
      $iframearguments[$argkey] = $options[$argkey];
    }
  }
  $options['external'] = TRUE;
  return '<div class="' . (isset($options['class']) ? check_plain($options['class']) : '') . '">' . (empty($text) ? '' : '<h3 class="iframe_title">' . ($options['html'] ? $text : check_plain($text)) . '</h3>') . '<iframe src="' . check_url(url($path, $options)) . '"' . drupal_attributes($iframearguments) . '>' . t('Your browser does not support iframes, but you can use the following link.') . ' ' . l('Link', url($path, $options), $options_link) . '</iframe>' . '</div>';
}

/**************************************************************************
 * Field Type API: Widget
 **************************************************************************/

/**
 * Implements hook_field_widget_info().
 * shows the "Widget" select for this special field type on the manage-fields page of content-type
 * The type of form element you would like to present to the user when creating this field in the test type.
 * (Step 1)
 */
function iframe_field_widget_info() {
  return array(
    'iframe_widget_url' => array(
      'label' => t('URL only'),
      'field types' => array(
        'iframe',
      ),
      'settings' => array(
        'size' => 255,
      ),
      'weight' => 1,
    ),
    'iframe_widget_urlheight' => array(
      'label' => t('URL with height'),
      'field types' => array(
        'iframe',
      ),
      'settings' => array(
        'size' => 255,
      ),
      'weight' => 2,
    ),
    'iframe_widget_urlwidthheight' => array(
      'label' => t('URL with width and height'),
      'field types' => array(
        'iframe',
      ),
      'settings' => array(
        'size' => 255,
      ),
      'weight' => 3,
    ),
  );
}

/**
 * Implements hook_field_widget_settings_form().
 */
function iframe_DISABLED_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  iframe_debug(1, 'iframe_field_widget_settings_form', array(
    'field' => $field,
    'instance' => $instance,
  ));
  $form = array();

  /*
    $form['field_widget_settings_form'] = array(
      '#type' => 'textfield',
      '#title' => t('field_widget_settings_form'),
      '#default_value' => $settings['width'],
      '#required' => TRUE,
      //'#element_validate' => array('element_validate_integer_positive'),
    );
  */
  return $form;
}

/**
 * Implements hook_field_widget_form().
 *    depends on the widget-type
 * Field XYZ sub-menu "Edit" (used in the middle for "default" values for each individual instance-item,
 *                            prefilled, with setting from iframe_field_info->instance_settings)
 * (Step 3)
 */
function iframe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  iframe_debug(2, 'iframe_field_widget_form', $form);
  $base = $element;
  $widget = $instance['widget'];

  #$settings = $widget['settings']; // size=255
  $settings = $instance['settings'];

  // for defaults
  // description of widget (help-text)
  switch ($instance['widget']['type']) {
    case 'iframe_widget_urlwidthheight':
      $element['#description'] = t('Set your iframe *URL* and the *width* and *height*.');
      break;
    case 'iframe_widget_urlheight':
      $element['#description'] = t('Set your iframe *URL* and the *height*.');
      break;
    case 'iframe_widget_url':
    default:
      $element['#description'] = t('Set your iframe *URL*.');
      break;
  }
  switch ($instance['widget']['type']) {
    case 'iframe_widget_urlwidthheight':
      $fwidth = isset($items[$delta]) && isset($items[$delta]['width']) ? $items[$delta]['width'] : (!empty($settings['width']) ? $settings['width'] : NULL);
      $element['width'] = array(
        '#type' => 'textfield',
        '#title' => t('width'),
        '#default_value' => $fwidth,
        '#size' => 4,
        '#maxlength' => 4,
        '#weight' => 4,
        '#delta' => $delta,
      );

    // no "break" intentionally, fall through
    case 'iframe_widget_urlheight':
      $fheight = isset($items[$delta]) && isset($items[$delta]['height']) ? $items[$delta]['height'] : (!empty($settings['height']) ? $settings['height'] : NULL);
      $element['height'] = array(
        '#type' => 'textfield',
        '#title' => t('height'),
        '#default_value' => $fheight,
        '#size' => 4,
        '#maxlength' => 4,
        '#weight' => 5,
        '#delta' => $delta,
        '#element_validate' => array(
          'element_validate_integer_positive',
        ),
      );

    // no "break" intentionally, fall through
    case 'iframe_widget_url':
    default:

      //
      break;
  }
  $title = isset($items[$delta]) && isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL;
  $element['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Iframe Title'),
    '#placeholder' => '',
    '#default_value' => $title,
    '#size' => 80,
    '#maxlength' => 1024,
    '#weight' => 2,
    '#delta' => $delta,
  );
  $furl = isset($items[$delta]) && isset($items[$delta]['url']) ? $items[$delta]['url'] : NULL;
  $element['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Iframe URL'),
    '#placeholder' => 'http://',
    '#default_value' => $furl,
    '#size' => 80,
    '#maxlength' => 1024,
    '#weight' => 1,
    '#delta' => $delta,
    '#element_validate' => array(
      'iframe_validate_url',
    ),
  );
  $fclass = isset($items[$delta]) && isset($items[$delta]['class']) ? $items[$delta]['class'] : NULL;
  $expose_class = isset($settings['expose_class']) ? $settings['expose_class'] : 1;
  if ($expose_class) {
    $element['class'] = array(
      '#type' => 'textfield',
      '#title' => t('Additional CSS Class'),
      '#default_value' => $fclass,
      '#description' => t('Multiple classes should be separated by spaces.<br />You can use a class "autoresize" if You want height-autoresizing for iframes of the same domain.'),
      '#size' => 80,
      '#weight' => 5,
      '#delta' => $delta,
    );
  }

  // Display the list of available placeholders if token module is installed.
  $tokensupport = !empty($settings['tokensupport']) ? (int) $settings['tokensupport'] : 0;
  if ($tokensupport > 0) {
    if (module_exists('token')) {
      $element['token_tree'] = array(
        '#theme' => 'token_tree_link',
        '#token_types' => array(
          'node',
          'user',
        ),
        '#weight' => 3,
      );
      $element['url']['#description'] = (!empty($element['url']['#description']) ? $element['url']['#description'] . " " : "") . t('Field does support tokens.');
      $element['title']['#description'] = (!empty($element['title']['#description']) ? $element['title']['#description'] . " " : "") . t('Field does support tokens.');
    }
    else {
      $element['token_tree'] = array(
        '#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array(
          '@drupal-token' => 'http://drupal.org/project/token',
        )) . '</p>',
      );
    }
  }
  iframe_debug(2, 'iframe_field_widget_form', array(
    'settings' => $settings,
    'instance-settings' => $instance['settings'],
  ));
  return $element;
}

/**
 * Implements hook_field_widget_error().
 */
function iframe_DISABLED_field_widget_error($element, $error) {
  switch ($error['error']) {
    case 'widthsize':
      form_error($element['width'], $error['message']);
      break;
    case 'heightsize':
      form_error($element['height'], $error['message']);
      break;
  }
}

/**
 * Function to validate the iframe URL.
 */
function iframe_validate_url($element, &$form_state) {
  if (!empty($element['#value'])) {
    if (preg_match('#(^http|\\://)#', $element['#value'])) {
      if (!valid_url($element['#value'], TRUE)) {
        form_error($element, t('Invalid iframe URL.'));
      }
    }
    else {

      # not absolute url
      if (!valid_url($element['#value'], FALSE)) {
        form_error($element, t('Invalid iframe URL.'));
      }
    }
  }
}

/**
 * Support for local-domain iframes and autoresize the height
 *
 * Implementation of hook_init().
 */
function iframe_init() {
  drupal_add_js(drupal_get_path('module', 'iframe') . '/iframe.js');
  if (!function_exists('iframe_content_migrate_field_alter')) {
    if (function_exists('module_load_include')) {
      module_load_include('inc', 'iframe', 'iframe.content_migrate.iframe');
    }
  }
  if (!function_exists('iframe_feeds_set_target')) {
    if (function_exists('module_load_include')) {
      module_load_include('inc', 'iframe', 'mappers/iframe');
    }
  }
}

Functions

Namesort descending Description
iframe_debug
iframe_DISABLED_field_widget_error Implements hook_field_widget_error().
iframe_DISABLED_field_widget_settings_form Implements hook_field_widget_settings_form().
iframe_field_attach_presave
iframe_field_formatter_info Implements hook_field_formatter_info(). used in field "manage display" for formatters (Step 4)
iframe_field_formatter_view Implements hook_field_formatter_view().
iframe_field_info Implements hook_field_info(). Shows the "Field type" while adding a new field to the content-type (Step 1)
iframe_field_instance_settings_form Implements hook_field_instance_settings_form(). These settings apply only to the iframe field when used in this special content-type. Field XYZ sub-menu "Edit" (Step 3)
iframe_field_is_empty Implements hook_field_is_empty().
iframe_field_presave Implements hook_field_presave().
iframe_field_settings_form Implements hook_field_settings_form(). These settings apply to the iframe field everywhere(!) it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created. Field XYZ sub-menu…
iframe_field_validate Implements hook_field_validate().
iframe_field_widget_form Implements hook_field_widget_form(). depends on the widget-type Field XYZ sub-menu "Edit" (used in the middle for "default" values for each individual instance-item, prefilled, with setting from…
iframe_field_widget_info Implements hook_field_widget_info(). shows the "Widget" select for this special field type on the manage-fields page of content-type The type of form element you would like to present to the user when creating this field in the test…
iframe_formatter_asurlwithuri_helper Formatter function for 'asurlwithuri' text field formatter.
iframe_formatter_asurl_helper Formatter function for 'asurl' text field formatter.
iframe_formatter_default_helper Formatter function for 'default' text field formatter.
iframe_formatter_hidden_helper Formatter function for 'hidden' text field formatter.
iframe_formatter_only_helper Formatter function for 'iframeonly' text field formatter.
iframe_help Implements hook_help().
iframe_iframe
iframe_init Support for local-domain iframes and autoresize the height
iframe_validate_url Function to validate the iframe URL.

Constants

Namesort descending Description
DEBUG_LEVEL
iframe_DOMAINS
iframe_EMAIL
iframe_EXTERNAL @file Defines an iframe field with all attributes.
iframe_FRONT
iframe_INTERNAL