You are here

private function FrxDrupal::loadEntities in Forena Reports 8

Perform a bulk load of entities.

Enttities are loaded 100 at a time and then join data back onto the simplexml already created.

Parameters

string $type: Type of entity being loaded.

array $entity_map: Map of entity ids to nodes.

1 call to FrxDrupal::loadEntities()
FrxDrupal::sqlData in src/FrxPlugin/Driver/FrxDrupal.php
Get data based on file data block in the repository.

File

src/FrxPlugin/Driver/FrxDrupal.php, line 186
Provides data blocks for native drupal connections using the default drupal connections.

Class

FrxDrupal
Class FrxDrupal

Namespace

Drupal\forena\FrxPlugin\Driver

Code

private function loadEntities($type, $entity_map, $select_fields, $display = array()) {

  // Do these 100 at a time for performance reasons
  $chunks = array_chunk($entity_map, 100, TRUE);
  foreach ($chunks as $chunk) {
    $ids = array_keys($chunk);
    $data = entity_load($type, $ids);
    if ($data) {
      foreach ($data as $id => $e) {

        // Get node permissions
        $access = $type == 'node' ? node_access('view', $e) : TRUE;
        if (isset($entity_map[$id])) {
          foreach ($entity_map[$id] as $entry) {
            $row_node = $entry;
            $lang = isset($e->language) ? $e->language : 'und';

            // remove elements we don't have access to
            if (!$access) {
              unset($row_node[0]);
            }
            else {
              foreach ($e as $key => $val) {
                if ($val && !isset($row_node->{$key})) {
                  if (strpos($key, 'field_') === 0) {

                    //$fields = field_get_items('node', $node, $key);
                    if (!$display) {
                      $display = 'default';
                    }
                    $field = field_view_field($type, $e, $key, $display);
                    $field['#theme'] = array(
                      'forena_inline_field',
                    );
                    $value = drupal_render($field);
                    $f = $row_node
                      ->addChild($key, htmlspecialchars($value));
                    if (isset($field['#field_type'])) {
                      $f['type'] = $field['#field_type'];
                    }
                    if (isset($field['#field_name'])) {
                      $f['name'] = $field['#field_name'];
                    }
                  }
                  else {
                    if (is_array($val) && isset($val[$lang])) {
                      $tmp = $val[$lang][0];
                      if (isset($tmp['safe_value'])) {
                        $row_node
                          ->addChild($key, htmlspecialchars($tmp['safe_value']));
                      }
                      else {
                        if (isset($tmp['value'])) {
                          $row_node
                            ->addChild($key, htmlspecialchars($tmp['value']));
                        }
                      }
                    }
                    else {
                      if (is_array($val) && isset($val['und'])) {
                        $tmp = $val['und'][0];
                        if (isset($tmp['safe_value'])) {
                          $row_node
                            ->addChild($key, htmlspecialchars($tmp['safe_value']));
                        }
                        else {
                          if (isset($tmp['value'])) {
                            $row_node
                              ->addChild($key, htmlspecialchars($tmp['value']));
                          }
                        }
                      }
                      else {
                        if (is_scalar($val)) {
                          $row_node
                            ->addChild($key, htmlspecialchars($val));
                        }
                      }
                    }
                  }
                }
              }

              // Now add on select fields
              if ($select_fields[$id]) {
                foreach ($select_fields[$id] as $key => $val) {
                  if (!isset($row_node->{$key})) {
                    $row_node
                      ->addChild($key, htmlspecialchars($val));
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}