Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGIR-130 : Error handling for islandora_citations Cite this block #60

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\islandora_citations\IslandoraCitationsHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -20,32 +21,46 @@ class SelectCslForm extends FormBase {
* @var Drupal\islandora_citations\IslandoraCitationsHelper
*/
protected $citationHelper;

/**
* CSL type value from block.
*
* @var string
*/
private $blockCSLType;

/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* {@inheritdoc}
*/
public function __construct(IslandoraCitationsHelper $citationHelper, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(IslandoraCitationsHelper $citationHelper,
RouteMatchInterface $route_match,
EntityTypeManagerInterface $entity_type_manager,
LoggerInterface $logger) {
$this->citationHelper = $citationHelper;
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger;
}

/**
Expand All @@ -55,7 +70,8 @@ public static function create(ContainerInterface $container) {
return new static(
$container->get('islandora_citations.helper'),
$container->get('current_route_match'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('logger.factory')->get('islandora_citations')
);
}

Expand Down Expand Up @@ -90,6 +106,28 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$default_csl = array_values($cslItems)[0];
}
$csl = !empty($default_csl) ? $this->getDefaultCitation($default_csl) : '';

// We receive error message as a string, and then we display same string
// as output.
// We expect output in a specific format when there is no error as below
// <div class="csl-bib-body">
// <div class="csl-entry">“Text_Output”</div>
// </div>.
// Based on `csl` text output, we will do the error handling.
// When HTML output is not as expected, add a form element which indicates
// we received error.
if (!str_starts_with($csl, '<div class="csl-bib-body">')) {
// Add a custom markup element to the form.
$form['error_handling_element'] = [
'#markup' => 'Form with error',
];

// Log error message.
$this->logger->error($csl);

return $form;
}

$form['csl_list'] = [
'#type' => 'select',
'#options' => $cslItems,
Expand Down Expand Up @@ -140,13 +178,19 @@ public function renderAjaxCitation(array $form, FormStateInterface $form_state)
'#children' => '',
];
}
// Method call to render citation.
$rendered = $this->renderCitation($csl_name);
$response = [
'#children' => $rendered['data'],
];

return $form['data'] = $response;
try {
// Method call to render citation.
$rendered = $this->renderCitation($csl_name);
$response = [
'#children' => $rendered['data'],
];

return $response;
}
catch (\Throwable $e) {
return $e->getMessage();
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/IslandoraCitationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ public function renderWithCiteproc(array $data, string $style, string $mode = 'b
* @throws \Exception
*/
public function encodeEntityForCiteproc(EntityInterface $entity): object {
$cslEncodedData = $this->serializer->normalize($entity, 'csl-json');
return (object) $cslEncodedData;
try {
$cslEncodedData = $this->serializer->normalize($entity, 'csl-json');
return (object) $cslEncodedData;
}
catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}

}
13 changes: 10 additions & 3 deletions src/Plugin/Block/DisplayCitationsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;

/**
* Citation helper service.
*
Expand Down Expand Up @@ -83,10 +84,16 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function build() {
if (!empty($this->citationHelper->getCitationEntityList())) :
$build['form'] = $this->formBuilder->getForm('Drupal\islandora_citations\Form\SelectCslForm');
$cite_this_form = $this->formBuilder->getForm('Drupal\islandora_citations\Form\SelectCslForm');
if (!empty($cite_this_form['error_handling_element'])) {
// Hide the entire block.
return NULL;
}

if (!empty($this->citationHelper->getCitationEntityList())) {
$build['form'] = $cite_this_form;
return $build;
endif;
}
}

/**
Expand Down