public function remote__data_formatter::view

3.x remote__data_formatter.inc public remote__data_formatter::view(&$element, $entity_type, $entity, $langcode, $items, $display)

Provides the display for a field

This function corresponds to the hook_field_formatter_view() function of the Drupal Field API.

This function provides the display for a field when it is viewed on the web page. The content returned by the formatter should only include what is present in the $items[$delta]['values] array. This way, the contents that are displayed on the page, via webservices and downloaded into a CSV file will always be identical. The view need not show all of the data in the 'values' array.

Parameters

$element: @param $entity_type @param $entity @param $langcode @param $items @param $display

@return An element array compatible with that returned by the hook_field_formatter_view() function.

Overrides TripalFieldFormatter::view

File

tripal_ws/includes/TripalFields/remote__data/remote__data_formatter.inc, line 71

Class

remote__data_formatter

Code

public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  // Get the settings
  $settings = $display['settings'];
  $field_name = $this->field['field_name'];

  // Get any subfields and the header label.  Shift the array because the
  // results should already be the value of the fisrt entry.
  $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
  $subfields = explode(',', $rd_field_name);
  $header_label = $this->getHeaderLabel($subfields);
  $flabel = array_shift($subfields);

  // Get the site logo if one is provided
  $site_logo = $this->instance['settings']['data_info']['site_logo'];
  if ($site_logo) {
    $site_logo = file_load($site_logo);
  }

  // Get the site name where the data came from.
  $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  $site = db_select('tripal_sites', 'ts')
    ->fields('ts', array('name', 'url'))
    ->condition('ts.id', $site_id_ws)
    ->execute()
    ->fetchObject();

  $content = '<p>';
  if (is_object($site_logo)) {
    $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
  }
  $content .= t('This content provided by !site.', 
  array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
  $content .= '</p>';
  $rows = array();
  foreach ($items as $index => $item) {
    $remote_entity_label = $item['remote_entity']['label'];
    $remote_entity_page = $item['remote_entity']['ItemPage'];
    $link = t('View !data on %site', 
    array('!data' => l('this content', $remote_entity_page, array('attributes' => array('target' => '_blank'))),
      '%site' => $site->name));
    $value = $item['value'];
    if (!$value) {
      continue;
    }
    $headers = array('');

    // If this is a collection then handle it as a list of members.
    if (array_key_exists('members', $value)) {
      foreach ($value['members'] as $subvalue) {
        $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
        $rows[] = array($subvalue);
      }
    }
    else {
      if (count($subfields) > 0) {
        $subvalue = $this->refineSubValue($value, $subfields, $header_label);
        $rows[] = array($subvalue);
      }
      else {
        if (array_key_exists($flabel, $value)) {
          $rows[] = array(l($value[$flabel], $remote_entity_page, array('attributes' => array('target' => '_blank'))));
        }
        else {
          $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
          $rows[] = array($value);
        }
      }
    }

  }

  $has_sub_tables = FALSE;
  for ($i = 0; $i < count($rows); $i++) {
    if (is_array($rows[$i][0])) {
      $rows[$i][0] = $this->createTable($rows[$i]);
      $has_sub_tables = TRUE;
    }
  }

  // If we don't have  tables for each row then we'll put everything into
  // a table.
  if (!$has_sub_tables) {
    $headers = array($header_label . '(s)');
    $content .= theme_table(array(
      'header' => $headers,
      'rows' => $rows,
      'attributes' => array(
        'class' => 'tripal-remote--data-field-table',
      ),
      'sticky' => FALSE,
      'caption' => "",
      'colgroups' => array(),
      'empty' => 'There are no results.',
    ));
  }
  else {
    for ($i = 0; $i < count($rows); $i++) {
      if (count($rows) > 1) {
        $content .= '<span class="tripal-remote--data-field-table-label">' . $header_label . ' ' . ($i + 1) . '</span>';
      }
      $content .= $rows[$i][0];
    }
  }
  // Return the content for this field.
  $element[0] = array(
    '#type' => 'markup',
    '#markup' => '<div class="tripal-remote--data-field">' . $content . '</div>',
  );
}