You are here

public function RestfulDataProviderCToolsPlugins::view in RESTful 7

@todo: We should generalize this, as it's repeated often.

Overrides RestfulBase::view

1 call to RestfulDataProviderCToolsPlugins::view()
RestfulDataProviderCToolsPlugins::index in plugins/restful/RestfulDataProviderCToolsPlugins.php
Get a list of entities.

File

plugins/restful/RestfulDataProviderCToolsPlugins.php, line 250
Contains \RestfulDataProviderCToolsPlugins

Class

RestfulDataProviderCToolsPlugins
@file Contains \RestfulDataProviderCToolsPlugins

Code

public function view($id) {
  $cache_id = array(
    'md' => $this
      ->getModule(),
    'tp' => $this
      ->getType(),
    'id' => $id,
  );
  $cached_data = $this
    ->getRenderedCache($cache_id);
  if (!empty($cached_data->data)) {
    return $cached_data->data;
  }
  if (!($plugin = ctools_get_plugins($this
    ->getModule(), $this
    ->getType(), $id))) {

    // Since the discovery resource sits under 'api/' it will pick up all
    // invalid paths like 'api/invalid'. If it is not a valid plugin then
    // return a 404.
    throw new \RestfulNotFoundException('Invalid URL path.');
  }

  // Loop over all the defined public fields.
  foreach ($this
    ->getPublicFields() as $public_field_name => $info) {
    $value = NULL;
    if ($info['create_or_update_passthrough']) {

      // The public field is a dummy one, meant only for passing data upon
      // create or update.
      continue;
    }

    // If there is a callback defined execute it instead of a direct mapping.
    if ($info['callback']) {
      $value = static::executeCallback($info['callback'], array(
        $plugin,
      ));
    }
    elseif ($info['property']) {
      $value = $plugin[$info['property']];
    }

    // Execute the process callbacks.
    if ($value && $info['process_callbacks']) {
      foreach ($info['process_callbacks'] as $process_callback) {
        $value = static::executeCallback($process_callback, array(
          $value,
        ));
      }
    }
    $output[$public_field_name] = $value;
  }
  $this
    ->setRenderedCache($output, $cache_id);
  return $output;
}