Skip to content

Instantly share code, notes, and snippets.

@anpolimus
Created January 5, 2018 10:41
Show Gist options
  • Select an option

  • Save anpolimus/da59e8ecb12ae5c6bfb550cd14f19a95 to your computer and use it in GitHub Desktop.

Select an option

Save anpolimus/da59e8ecb12ae5c6bfb550cd14f19a95 to your computer and use it in GitHub Desktop.
Data Export style example
<?php
namespace Drupal\bridge_search\Plugin\views\style;
use Drupal\views_data_export\Plugin\views\style\DataExport;
use Drupal\Core\Url;
/**
* A style plugin for data export views at Bridgetodata XLS export.
*
* @ingroup views_style_plugins
*
* @ViewsStyle(
* id = "bridge_search_data_export",
* title = @Translation("Bridge:Data export"),
* help = @Translation("Configurable row output for data exports."),
* display_types = {"data"}
* )
*/
class BridgeDataExportStyle extends DataExport {
/**
* {@inheritdoc}
*/
public function render() {
$rows = [];
$exposed_input = $this->view->getExposedInput();
$search_criteria_count = 0;
foreach ($exposed_input as $key => $value) {
if (strpos($key, 'field_')!==FALSE) {
if (!((empty($value) || ($value == 'All')))) {
$search_criteria_count++;
}
}
}
foreach ($this->view->result as $row_index => $row) {
$this->view->row_index = $row_index;
// Generate Percentage Match column.
$renderer = \Drupal::service('renderer');
$rmatch = [
'#markup' => round($row->rmatch/$search_criteria_count*100,2) . '%'
];
$rmatch_render = $renderer->render($rmatch);
// Generate link to the node column.
$options = ['absolute' => TRUE];
$url_object = [
'#markup' => Url::fromRoute('entity.node.canonical', ['node' => $row->nid], $options)->toString()
];
$url_object_render = $renderer->render($url_object);
$render_row = $this->view->rowPlugin->render($row);
$render_row = $this->arrayInsertAfter('title', $render_row, 'match', $rmatch_render);
$render_row = $this->arrayInsertAfter('match', $render_row, 'link', $url_object_render);
$render_row = $this->updateLabels($render_row);
$rows[] = $render_row;
}
unset($this->view->row_index);
// Get the content type configured in the display or fallback to the
// default.
if ((empty($this->view->live_preview))) {
$content_type = $this->displayHandler->getContentType();
}
else {
$content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
}
return $this->serializer->serialize($rows, $content_type, ['views_style_plugin' => $this]);
}
/**
* Helper function that updates column titles for data export.
* @param $row
* @return array
*/
public function updateLabels($row) {
$string_alter = $this->titleMapping();
$new_row = [];
foreach ($row as $column_label => $row_item) {
if (isset($string_alter[$column_label])) {
$column_label = $string_alter[$column_label];
}
$new_row[$column_label] = $row_item;
}
return $new_row;
}
/**
* Helper function that process array manipulation
* @param $key
* @param array $array
* @param $new_key
* @param $new_value
* @return array|bool
*/
public function arrayInsertAfter($key, array &$array, $new_key, $new_value) {
if (array_key_exists ($key, $array)) {
$new = array();
foreach ($array as $k => $value) {
$new[$k] = $value;
if ($k === $key) {
$new[$new_key] = $new_value;
}
}
return $new;
}
return FALSE;
}
/**
* Field titles mapping.
*/
public static function titleMapping() {
return [
'title' => 'Database Name',
'match' => 'Relevancy Rank',
'link' => 'Page',
'field_coordinating_country' => 'Country',
'field_region' => 'Region',
'field_database_type' => 'Database Type',
'field_diagnosis_data' => 'Diagnosis Data',
'field_diagnoses_coded' => 'Diagnoses Coded',
'field_procedure_data' => 'Procedure Data',
'field_procedures_coded' => 'Procedures Coded',
'field_drug_data' => 'Drug Data',
'field_drug_coding_system_primary' => 'Drug Coding System: Primary'
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment