You are here

public function Record::render in Views OAI-PMH 8

Render the display in this style.

Overrides StylePluginBase::render

File

src/Plugin/views/style/Record.php, line 177

Class

Record
Plugin annotation @ViewsStyle( id = "views_oai_pmh_record", title = @Translation("OAI-PMH"), help = @Translation("Displays rows in OAI-PMH records."), display_types = {"oai_pmh"} )

Namespace

Drupal\views_oai_pmh\Plugin\views\style

Code

public function render() {
  $rows = $this
    ->getResultRows();

  /** @var \Drupal\views_oai_pmh\Plugin\MetadataPrefixInterface $currentPrefixPlugin */
  $currentPrefixPlugin = $this->prefixManager
    ->createInstance($this->displayHandler
    ->getCurrentMetadataPrefix());
  $records = [];
  foreach ($rows as $row_id => $row) {
    $this->rowToXml
      ->resetTagsPrefixedWith0();

    // Getting all the elements.
    $elements = $this->rowToXml
      ->transform($row);
    $element_or_null = array_shift($elements);

    // Insure we're adding (union) arrays, not null.
    $element = $element_or_null ? $element_or_null : array();

    // Getting only the root elements, we need to remove the root elements
    // from the list with all the elements to prepare the correct structure.
    $root_elements = $element + $currentPrefixPlugin
      ->getRootNodeAttributes();

    // Getting the elements in the level the need to be.
    $data = $root_elements + $elements;

    // path id for datacite, dcc or dc
    $path_id = !empty($data['identifier']) ? $data['identifier']['#'] : $data['dc:identifier']['#'];
    $xmlDoc = new \DOMDocument();
    $xmlDoc
      ->loadXML($this->serializer
      ->encode($data, 'xml', [
      'xml_root_node_name' => $currentPrefixPlugin
        ->getRootNodeName(),
    ]));

    // Removing empty elements.
    $xpath = new \DOMXPath($xmlDoc);
    foreach ($xpath
      ->query('//*[not(node())]') as $node) {
      $node->parentNode
        ->removeChild($node);
    }
    $header = new Header($this
      ->getIdentifier($path_id), new \DateTime());
    $records[$row_id] = new OAIRecord($header, $xmlDoc);
  }
  $formats = [];
  foreach ($this->options['enabled_formats'] as $format) {
    if ($format) {
      $plugin = $this
        ->getInstancePlugin($format);
      $formats[] = new MetadataFormatType($format, $plugin
        ->getSchema(), $plugin
        ->getNamespace());
    }
  }
  $this->repository
    ->setRecords($records);
  if ($pager = $this->view->pager
    ->hasMoreRecords()) {
    $this->repository
      ->setOffset($this->view
      ->getCurrentPage() + 1);
    $this->repository
      ->setTotalRecords($this->view->total_rows);
  }
  $this->repository
    ->setMetadataFormats($formats);
  return $this->provider;
}