You are here

public function WsBean::view in Web Service Data 7

Displays the bean.

Overrides BeanPlugin::view

File

modules/wsbeans/plugins/beans/wsbeans_wsbean.inc, line 152
Listing bean plugin.

Class

WsBean
@file Listing bean plugin.

Code

public function view($bean, $content, $view_mode = 'default', $langcode = NULL) {
  $contexts = NULL;
  if (isset($bean->contexts)) {
    $contexts = $bean->contexts;
  }
  if (!empty($bean->settings['themehook'])) {
    $form = array(
      '#theme' => $bean->settings['themehook'],
    );
  }
  $wsconfig = wsconfig_load_by_name($bean->settings['wsconfig_settings']['wsconfig']);
  if (!$wsconfig) {
    $content['bean'][$bean->delta]['form']['#markup'] = t('Unable to load WSConfig @wsconfig', array(
      '@wsconfig' => $bean->settings['wsconfig'],
    ));
    return $content;
  }
  $processor = $bean->settings['wsprocessor'];
  if (!class_exists($processor) or !is_subclass_of($processor, 'WsData')) {
    $content['bean'][$bean->delta]['form']['#markup'] = t('Unable to load wsprocessor @wsprocessor', array(
      '@wsprocessor' => $bean->settings['wsprocessor'],
    ));
    return $content;
  }
  $method = 'wsconfig_method_' . $bean->settings['wsconfig_settings']['wsconfig'];

  // Fidn the replacements.
  $replacements = array();
  foreach ($wsconfig
    ->getReplacements($bean->settings['wsconfig_settings'][$method]) as $arg) {
    $replacements[$arg] = $bean->settings['wsconfig_settings']['replacements'][$method][$arg . '_default'];

    // If contexts are available, find the matching one and replace it.
    if (isset($contexts)) {
      $context_option = $bean->settings['wsconfig_settings']['replacements'][$method][$arg];
      list($type, $converter) = explode('.', $context_option);
      foreach ($contexts[1] as $context) {
        if ($context->type == $type) {
          $replacements[$arg] = ctools_context_convert_context($context, $converter);
          break;
        }
      }
    }
  }
  $arguments = json_decode($bean->settings['wsconfig_settings']['arguments'], TRUE);
  $arguments = isset($arguments) ? $arguments : array();
  $options = json_decode($bean->settings['wsconfig_settings']['options'], TRUE);
  $options = isset($options) ? $options : array();
  try {
    $data = $wsconfig
      ->call($bean->settings['wsconfig_settings'][$method], $replacements, $arguments, $options);
  } catch (Exception $e) {
    $content['bean'][$bean->delta]['form']['#markup'] = t('An error occured while making the web service call.');
    return $content;
  }

  // Create a new processor
  $processor = new $processor($data, $bean);
  $content['bean'][$bean->delta]['form'] = $processor
    ->getData();
  return $content;
}