You are here

function opengraph_meta_form_alter in Open Graph meta tags 6

Same name and namespace in other branches
  1. 7.2 opengraph_meta.module \opengraph_meta_form_alter()
  2. 7 opengraph_meta.module \opengraph_meta_form_alter()

Implementation of hook_form_alter.

File

./opengraph_meta.module, line 47

Code

function opengraph_meta_form_alter(&$form, $form_state, $form_id) {

  // if editing a node
  if ('node_form' == stristr($form_id, 'node_form')) {
    $node = $form['#node'];

    // check that tags are enabled for this node type and that user has permission to edit them
    if (!OpenGraphMeta::instance()
      ->tags_are_enabled_for_content_type($node->type) || !user_access(OPENGRAPH_META_PERM_EDIT)) {
      return;
    }

    // add meta tags editing for making it easier to share on Facebook
    $form['opengraph_meta'] = array(
      '#type' => 'fieldset',
      '#title' => t('Open Graph meta tags (e.g. for Facebook sharing)'),
      '#description' => t('Here you can specify the exact title and summary text for this node as it will appear when shared on e.g. Facebook'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 100,
      OpenGraphMeta::TITLE => array(
        '#title' => t('Title'),
        '#type' => 'textfield',
        '#maxlength' => 255,
        '#default_value' => !empty($node->opengraph_meta) ? $node->opengraph_meta[OpenGraphMeta::TITLE] : '',
        '#description' => t('The title of the node. If left blank then the node title will be used.'),
      ),
      OpenGraphMeta::DESCRIPTION => array(
        '#title' => t('Summary'),
        '#type' => 'textarea',
        '#default_value' => !empty($node->opengraph_meta) ? $node->opengraph_meta[OpenGraphMeta::DESCRIPTION] : '',
        '#description' => t('The summary of the node. If left blank then the first 200 characters of the node body text will be used.'),
      ),
      OpenGraphMeta::TYPE => array(
        '#title' => t('Type'),
        '#type' => 'select',
        '#options' => OpenGraphMeta::instance()
          ->get_all_og_types_for_select_field(),
        '#default_value' => !empty($node->opengraph_meta) ? $node->opengraph_meta[OpenGraphMeta::TYPE] : '',
        '#description' => t('The type of the node. if left unset then the value set for the %type content type will be used: "@setting"', array(
          '%type' => $node->type,
          '@setting' => variable_get(OPENGRAPH_META_VAR_CONTENT_TYPE_ . $node->type, ''),
        )),
      ),
    );

    // Location stuff
    $form['opengraph_meta']['location'] = array(
      '#type' => 'fieldset',
      '#title' => t('Open Graph Location tags'),
      '#description' => t('Here you can specify the location information.'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 100,
    );
    $form['opengraph_meta']['location'] = array_merge($form['opengraph_meta']['location'], _opengraph_meta_location_form_fields($node));

    // Contact stuff
    $form['opengraph_meta']['contact'] = array(
      '#type' => 'fieldset',
      '#title' => t('Open Graph Contact tags'),
      '#description' => t('Here you can specify the contact information.'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 100,
    );
    $form['opengraph_meta']['contact'] = array_merge($form['opengraph_meta']['contact'], _opengraph_meta_contact_form_fields($node));

    // if we have images in this node then show thumbnail selector
    $images = OpenGraphMeta::instance()
      ->harvest_images_from_node($node);
    if (!empty($images)) {
      $image_selector_options = array();
      foreach ($images as $image) {
        $image_selector_options[$image['url']] = OpenGraphMetaDrupalLayer::theme_selector_image($image);
      }
      $form['opengraph_meta'][OpenGraphMeta::IMAGE] = array(
        '#title' => t('Thumbnail image'),
        '#type' => 'radios',
        '#default_value' => !empty($node->opengraph_meta) ? $node->opengraph_meta[OpenGraphMeta::IMAGE] : '',
        '#description' => t('The thumbnail image that will get shown in e.g. a Facebook preview. If left unset then then the first available image will be used. If none available then the global fallback image will be used.'),
        '#options' => $image_selector_options,
        '#attributes' => array(
          'class' => array(
            'opengraph-thumbs-wrapper',
            'clearfix',
          ),
        ),
      );
    }
  }
}