You are here

opengraph_meta_upload.module in Open Graph meta tags 7.2

Adds an image upload field to the open graph meta tags field set.

File

opengraph_meta_upload/opengraph_meta_upload.module
View source
<?php

/**
 * @file
 * Adds an image upload field to the open graph meta tags field set.
 */

/**
 * Implements hook_form_FORM_ID_alter().
 */
function opengraph_meta_upload_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (!isset($form['opengraph_meta_image']) || !isset($form['opengraph_meta'])) {
    return;
  }
  $form += [
    '#pre_render' => [],
  ];
  array_unshift($form['#pre_render'], 'opengraph_meta_image_pre_render');

  // Add our submit handler between the opengraph_meta and the
  // metatags submit handler.
  $submit = [];
  foreach ($form['#submit'] as $func) {
    if ($func == 'opengraph_meta_node_form_submit') {
      $submit[] = 'opengraph_meta_image_node_form_submit';
    }
    $submit[] = $func;
  }
  $form['#submit'] = $submit;
  $radio_field =& $form['opengraph_meta']['image'];
  $upload_field =& $form['opengraph_meta_image'];

  // Add "upload" as the second option.
  $options = array_slice($radio_field['#options'], 0, 1, TRUE);
  $options['upload'] = t('Upload an image.');
  $options += array_slice($radio_field['#options'], 1, NULL, TRUE);
  $radio_field['#options'] = $options;

  // Hide the upload field if "upload" is not selected.
  $upload_field['#states']['visible'][':input[name="opengraph_meta[image][select]"]']['value'] = 'upload';
  $upload_item =& $upload_field['und'][0];
  if (!empty($upload_item['#default_value']['uri'])) {
    if ($radio_field['#default_value'] == $upload_item['#default_value']['uri']) {

      // The uploaded image is selected.
      $radio_field['#default_value'] = 'upload';
    }

    // Remove the uploaded image from the list of other images.
    unset($radio_field['#options'][$upload_item['#default_value']['uri']]);
  }
}

/**
 * Pre_render function for opengraph_meta_image.
 */
function opengraph_meta_image_pre_render(&$element) {
  $element['opengraph_meta']['opengraph_meta_image'] = $element['opengraph_meta_image'];
  $element['opengraph_meta']['opengraph_meta_image']['#weight'] = $element['opengraph_meta']['type']['#weight'] + 1;
  unset($element['opengraph_meta_image']);
  return $element;
}

/**
 * Update metatags with the URL of the uploaded image.
 */
function opengraph_meta_image_node_form_submit(&$form, &$form_state) {
  $upload_field =& $form_state['values']['opengraph_meta_image'];
  $select_field =& $form_state['values']['opengraph_meta']['image'];
  if ($select_field == 'upload') {

    // Set og:image to the URL of the uploaded image.
    if (isset($upload_field['und'][0]['fid']) && $upload_field['und'][0]['fid'] > 0) {
      $file = file_load($upload_field['und'][0]['fid']);
      $select_field = $file->uri;
    }
  }
}

Functions

Namesort descending Description
opengraph_meta_image_node_form_submit Update metatags with the URL of the uploaded image.
opengraph_meta_image_pre_render Pre_render function for opengraph_meta_image.
opengraph_meta_upload_form_node_form_alter Implements hook_form_FORM_ID_alter().