eternalistic
3/25/2015 - 9:16 PM

Opengraph

Opengraph

/**
 * hook_preprocess_html()
 */
function asp3_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'asp_theme';

  // Set OG metatags for Facebook.
  
  // Exhibit Entities
  if ( strpos(current_path(),'exhibit/exhibit') !== FALSE ) {
    $entity_id = substr(strrchr(current_path(), "/"), 1);
    $entity_array = entity_load('exhibit', array($entity_id));

    if ( count($entity_array) > 0 ) {
      $entity = reset($entity_array);
      // Image
      $fid = $entity->field_signature_image['und'][0]['fid'];
      $file = file_load($fid);
      $uri = $file->uri;
      $sig_image_url = file_create_url($uri);
      $vars['og_image'] = $sig_image_url;
      // Title
      $vars['og_title'] = $entity->title;
      // Description
      $vars['og_description'] = strip_tags( $entity->field_short_description['und'][0]['value'] );
      $vars['og_image_type'] = 'image/jpeg';
      $vars['og_image_width'] = 564;
      $vars['og_image_height'] = 380;
    }
  }

  // Event Entities
  if ( strpos(current_path(),'event/event') !== FALSE ) {
    $entity_id = substr(strrchr(current_path(), "/"), 1);
    $entity_array = entity_load('event', array($entity_id));

    if ( count($entity_array) > 0 ) {
      $entity = reset($entity_array);
      // Image
      $fid = $entity->field_signature_image['und'][0]['fid'];
      $file = file_load($fid);
      $uri = $file->uri;
      $sig_image_url = file_create_url($uri);
      $vars['og_image'] = $sig_image_url;
      // Title
      $vars['og_title'] = $entity->title;
      // Description
      $vars['og_description'] = strip_tags( $entity->field_short_description['und'][0]['value'] );
      $vars['og_image_type'] = 'image/jpeg';
      $vars['og_image_width'] = 564;
      $vars['og_image_height'] = 380;
    }
  }

  // Video Entities.
  if ( strpos(current_path(),'video/video') !== FALSE ) {
    $entity_id = substr(strrchr(current_path(), "/"), 1);
    $entity_array = entity_load('video', array($entity_id));

    if ( count($entity_array) > 0 ) {
      $entity = reset($entity_array);
      
      // Focus.
      $focus = $entity->field_video_focus['und']['0']['value'];

      // Change image and description based on Focus.
      if ($focus == 'Freeform') {
        $vars['og_description'] = strip_tags($entity->field_descriptor['und'][0]['safe_value']);
        $fid = $entity->field_signature_image['und'][0]['fid'];
      }
      elseif ($focus == 'Event') {
        
        



        
        
        $vars['og_description'] = NULL;
        $fid = NULL;
      }
      $file = file_load($fid);
      $uri = $file->uri;
      $sig_image_url = file_create_url($uri);
      $vars['og_image'] = $sig_image_url;
      // Title.
      $vars['og_title'] = entity_label('video', $entity);
      // Description.
      $vars['og_image_type'] = 'image/jpeg';
      $vars['og_image_width'] = 564;
      $vars['og_image_height'] = 380;
    }  
  }

  // People Entities
  if ( strpos(current_path(),'people/people') !== FALSE ) {
    $entity_id = substr(strrchr(current_path(), "/"), 1);
    $entity_array = entity_load('people', array($entity_id));

    if ( count($entity_array) > 0 ) {
      $entity = reset($entity_array);
      // Image
      $fid = $entity->field_signature_image['und'][0]['fid'];
      $file = file_load($fid);
      $uri = $file->uri;
      $sig_image_url = file_create_url($uri);
      $vars['og_image'] = $sig_image_url;
      // Title
      $vars['og_title'] = entity_label('people', $entity);
      // Description
      $vars['og_description'] = strip_tags($entity->field_biography['und'][0]['value'] );
      $vars['og_image_type'] = 'image/jpeg';
      $vars['og_image_width'] = 564;
      $vars['og_image_height'] = 380;
    }
  }

  // Non-entities
  if ( !(strpos(current_path(),'exhibit/exhibit') !== FALSE) &&
       !(strpos(current_path(),'event/event') !== FALSE) &&
       !(strpos(current_path(),'video/video') !== FALSE) &&
       !(strpos(current_path(),'people/people') !== FALSE) ) {

      $vars['og_image'] = url('/sites/all/themes/asp3/images/logo.png', array('absolute' => true));
      $vars['og_title'] = $vars['head_title'];
      $vars['og_description'] = t('The Annenberg Space for Photography is a cultural destination dedicated to exhibiting both digital and print photography');
      $vars['og_image_type'] = 'image/jpeg';
      $vars['og_image_width'] = 564;
      $vars['og_image_height'] = 380;
  }
}