Skip to content

Commit

Permalink
Implement Twig best practices for ebsco result display #16
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilson committed Aug 28, 2018
1 parent f6c435f commit 69c741f
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 130 deletions.
173 changes: 44 additions & 129 deletions ebsco.module
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
* limitations under the License.
*/

use Symfony\Component\HttpFoundation\RedirectResponse;

use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;


require_once __DIR__ . '/lib/EBSCODocument.php';
Expand Down Expand Up @@ -1151,163 +1155,74 @@ function template_preprocess_ebsco_advanced_search(&$variables) {
* @see ebsco-result.tpl.php
*/
function template_preprocess_ebsco_result(&$variables) {

$params = $_REQUEST;
$params['op'] = isset($params['op']) ? $params['op'] : 'Next';

$_SESSION['EBSCO']['redirect'] = drupal_get_destination();
$destination = \Drupal::destination()->getAsArray();

$_SESSION['EBSCO']['redirect'] = $destination;
if (empty($Document)) {
$Document = new EBSCODocument();
}
$Document->retrieve();
$record = $Document->record();

$variables['record'] = $record;
$lastSearch['count'] = $Document->record_count();
$variables['count'] = $Document->record_count();

$lastSearch = isset($params['id']) ? $Document->search_read($params['id'], $params['op']) : 'fail';
$variables['last_search'] = $_SESSION['EBSCO']['lastsearch'];
$detailRecordHTML="";

if (isset($record) ){

$id = \Drupal\Component\Utility\Html::escape($record->record_id());
$fulltextUrl= \Drupal\Core\Url::fromRoute('ebsco.fulltext', array('id' => $id))->toString();;
$pdfUrl= \Drupal\Core\Url::fromRoute('ebsco.pdf', array('id' => $id))->toString();

$variables['record'] = $record = $Document->record();

$detailRecordHTML='<div class="ebsco-record detailrecord-2columns">'
.'<div>'
.'<div class="detailrecord-links"> '
if (isset($record)) {
$link_parameters = [
'id' => Html::escape($record->record_id())
];

.'<ul class="external-links">'
;
if($record->p_link){
$detailRecordHTML.= "<li> <a href='" . $record->p_link . "'>" . t('View in EDS') . "</a></li>";
if ($record->p_link) {
$variables['p_link_url'] = $record->p_link;
}

if ($record->pdf_link){
$detailRecordHTML.= '
<li>
<a href="' . $pdfUrl . '" class="icon pdf fulltext">' . t('PDF full text') . '</a>
</li>';
if ($record->pdf_link) {
$variables['pdf_link_url'] = Url::fromRoute('ebsco.pdf', $link_parameters)->toString();
}

if ($record->full_text_availability){
$detailRecordHTML.= '
<li>
<a href="' . ((!\Drupal::currentUser()->isAuthenticated()) ? $fulltextUrl : "") . '#html" class="icon html fulltext">' . t('HTML full text') . '</a>
</li>';
}
if ($record->full_text_availability) {
$variables['fulltext_link_url'] = Url::fromRoute('ebsco.fulltext', $link_parameters, ['fragment' => 'html'])->toString();

if (!empty($record->custom_links)){
foreach ($record->custom_links as $link){
$detailRecordHTML.= '
<li>
<a href="' . $link['Url'] . '" target="_blank" title="' . $link['MouseOverText'] . '" class="external-link">' . ($link['Icon']) ? '<img src="' . $link['Icon'] . '" />' : '' . $link['Name'] . '</a>
</li>';
// Simplify the link if the user is logged in already.
if (\Drupal::currentUser()->isAuthenticated()) {
$variables['fulltext_link_url'] = '#html';
}
}
$detailRecordHTML.='</ul>'
;

$variables['login_link'] = Link::createFromRoute(t('Login'), 'user.login', [], ['query' => $destination, 'fragment' => 'html'])->toString();

$detailRecordHTML.='</div>'
$lastSearch = isset($params['id']) ? $Document->search_read($params['id'], $params['op']) : [];
if (!empty($lastSearch)) {
$variables['last_search'] = $lastSearch;
$variables['current_index'] = $lastSearch['current_index'];


.'<div class="detailrecord-recordmetadata">'
.'<div class="record-toolbar">'
;
if ($lastSearch){
$detailRecordHTML.='<div class="floatright">';
if ($lastSearch['previous']){
$previousUrl= \Drupal\Core\Url::fromRoute('ebsco.detailedrecord', array('id' => $lastSearch['previous']->record_id, 'op' => 'Previous'))->toString();
$detailRecordHTML.= '<a href="' . $previousUrl. '" class="_record_link">&laquo; ' . t('Previous') . '</a> ';
if (!empty($_SESSION['search_total'])) {
$variables['last_search_total'] = $_SESSION['search_total'];
}
$detailRecordHTML.= $lastSearch['current_index'].' of ' . $_SESSION['search_total'];

if ($lastSearch['next']!==''){
$nextUrl= \Drupal\Core\Url::fromRoute('ebsco.detailedrecord', array('id' => $lastSearch['next']->record_id, 'op' => 'Next'))->toString();
$detailRecordHTML.= ' <a href="' . $nextUrl . '" class="_record_link">' . t('Next') . ' &raquo;</a>';
if ($lastSearch['previous']) {
$variables['previous_url'] = Url::fromRoute('ebsco.detailedrecord', array('id' => $lastSearch['previous']->record_id, 'op' => 'Previous'))->toString();
}
$detailRecordHTML.='</div>'
;
if (isset($_SESSION['EBSCO']['lastsearch']["destination"])) {
$detailRecordHTML.'<div class="floatleft">'
.'<a href="'.$_SESSION['EBSCO']['lastsearch']["destination"].'"> &laquo; '.t('Back to Results list').'</a>'
.'</div>'
.'<div class="clear"></div>'
;
}
}
$detailRecordHTML.='</div>';


$detailRecordHTML.='<div class="span-13">'
.' <table cellpadding="2" cellspacing="0" border="0" class="citation" summary="'.t('Bibliographic Details').'">'
;
foreach ($record->items as $item){
if (!empty($item['Data'])){
$detailRecordHTML.='<tr valign="top">'
.'<th width="150">' . t($item['Label']) . ':</th>'
.'<td>'. auto_link($item['Data']) . '</td>'
.'</tr>';
if ($lastSearch['next'] !== '') {
$variables['next_url'] = Url::fromRoute('ebsco.detailedrecord', array('id' => $lastSearch['next']->record_id, 'op' => 'Next'))->toString();
}
}

if ($record->db_label){
$detailRecordHTML.='<tr valign="top">'
.'<th width="150">' . t('Database') . ':</th><td>' . \Drupal\Component\Utility\Html::escape($record->db_label) . '</td>'
.'</tr>';
}

if ($record->full_text){
$detailRecordHTML.='<tr id="html" valign="top">'
.'<td colspan="2" class="html">' . $record->full_text . '</td>'
.'</tr>';
}
elseif ($record->access_level && !\Drupal::currentUser()->isAuthenticated()){

$link = '<a href="' . \Drupal\Core\Url::fromRoute('user.page')->toString() . '">' . t('Login') . '</a>';
$detailRecordHTML.='<tr id="html" valign="top">'
.'<td colspan="2" class="html">'
.'<p>' . t('The full text cannot be displayed to guests.') . '<br />'
.'<strong>' . sprintf(t('%s for full access.'), $link) . '</strong>'
.'</p>'
.'</td>'
.'</tr>';
if (isset($_SESSION['EBSCO']['lastsearch']['destination'])) {
$variables['backlink_url'] = $_SESSION['EBSCO']['lastsearch']['destination'];
}
}
$detailRecordHTML.='</table>'
.'</div>'
;

$detailRecordHTML.='<div class="span-4">';
if ($record->medium_thumb_link){
$detailRecordHTML.='<img src="' . check_url($record->medium_thumb_link) . '" class="book-jacket" alt="' . t('Book jacket') . '"/>';
foreach ($record->items as &$item) {
if (!empty($item['Data'])) {
$item['Auto_Link'] = auto_link($item['Data']);
}
}

if ($record->publication_type){
$detailRecordHTML.= '<p>' . \Drupal\Component\Utility\Html::escape($record->publication_type) . '</p>';
$request = \Drupal::request();
if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
$route->setDefault('_title', $record->title);
}
$detailRecordHTML.='</div>'

.'</div>'
.'</div>'
;

$detailRecordHTML.='<div class="clear"></div>';

$detailRecordHTML.='</div>';
}
$variables['detailrecord'] = $detailRecordHTML;




$request = \Drupal::request();
if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
$route->setDefault('_title', $record->title);
}
}

Expand Down Expand Up @@ -1519,4 +1434,4 @@ function auto_link($string)
$string
);
return $linkedString;
}
}
101 changes: 100 additions & 1 deletion templates/ebsco-result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* @see template_preprocess_ebsco_result()
*
* @ingroup themeable
*
* Copyright [2017] [EBSCO Information Services]
*
Expand All @@ -21,7 +22,105 @@
* limitations under the License.
*/
#}
<div class="ebsco-record detailrecord-2columns">
<div class="detailrecord-links">
<ul class="external-links">
{% if p_link_url is not empty %}
<li><a href="{{ p_link_url }}">{% trans %}View in EDS{% endtrans %}</a></li>
{% endif %}

{% if pdf_link_url is not empty %}
<li><a href="{{ pdf_link_url }}" class="icon pdf fulltext">{% trans %}PDF full text{% endtrans %}</a></li>
{% endif %}

{{ detailrecord |raw }}
{% if fulltext_link_url is not empty %}
<li><a href="{{ fulltext_link_url }}" class="icon html fulltext">{% trans %}HTML full text{% endtrans %}</a></li>
{% endif %}

{% if record.custom_links is not empty %}
{% for link in record.custom_links %}
<li><a href="{{ link.Url }}" target="_blank" title="{{ link.MouseOverText }}" class="external-link">
{% if link.Icon %}
<img src="{{ link.Icon }}" alt="{{ link.Name }}"/>
{% else %}
{{ link.Name }}
{% endif %}
</a></li>
{% endfor %}
{% endif %}
</ul>
</div>
<div class="detailrecord-recordmetadata">
<div class="record-toolbar">
{% if last_search %}
<div class="floatright">
{% if previous_url %}
<a href="{{ previous_url }}" class="_record_link">{% trans %}&laquo; Previous{% endtrans %}</a>
{% endif %}

{% if last_search_total %}
{% trans %}{{ current_index }} of {{ last_search_total }}{% endtrans %}
{% endif %}

{% if next_url %}
<a href="{{ next_url }}" class="_record_link">{% trans %}Next &raquo;{% endtrans %}</a>
{% endif %}
</div>
{% endif %}

{% if backlink_url %}
<div class="floatleft">
<a href="{{ backlink_url }}">{% trans %}&laquo; Back to Results list{% endtrans %}</a>
</div>
{% endif %}
<div class="clear"></div>
</div>

<div class="span-13">
<table cellpadding="2" cellspacing="0" border="0" class="citation" summary="{% trans %}Bibliographic Details{% endtrans %}">
{% for item in record.items %}
<tr valign="top">
<th width="150">{{ item.Label }}:</th>
<td>{{ item.Auto_Link|raw }}</td>
</tr>
{% endfor %}

{% if record.db_label is not empty %}
<tr valign="top">
<th width="150">{% trans %}Database{% endtrans %}:</th>
<td>{{ record.db_label }}</td>
</tr>
{% endif %}

{% if record.full_text is not empty %}
<tr valign="top" id="html">
<td colspan="2" class="html">{{ record.full_text }}</td>
</tr>
{% elseif record.access_level and not logged_in %}
<tr valign="top" id="html">
<td colspan="2" class="html">
<p>
{% trans %}The full text cannot be displayed to guests.{% endtrans %}
<br />
<strong>{% trans %}{{ login_link }} for full access.{% endtrans %}</strong>
</p>
</td>
</tr>
{% endif %}
</table>
</div>

<div class="span-4">
{% if record.medium_thumb_link %}
<img src="{{ record.medium_thumb_link }}" class="book-jacket" alt="{% trans %}Book jacket{% endtrans %}"/>
{% endif %}

{% if record.publication_type %}
<p>{{ record.publication_type }}</p>
{% endif %}
</div>

<div class="clear"></div>

</div>
</div>

0 comments on commit 69c741f

Please sign in to comment.