diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php
index 6a2b2cc..6552aeb 100644
--- a/src/Form/SelectCslForm.php
+++ b/src/Form/SelectCslForm.php
@@ -9,6 +9,7 @@
use Drupal\Core\Url;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\islandora_citations\IslandoraCitationsHelper;
+use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -22,18 +23,21 @@ 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.
*
@@ -48,17 +52,26 @@ class SelectCslForm extends FormBase {
*/
protected $pathAliasManager;
+ /**
+ * The logger service.
+ *
+ * @var \Psr\Log\LoggerInterface
+ */
+ protected $logger;
+
/**
* {@inheritdoc}
*/
public function __construct(IslandoraCitationsHelper $citationHelper,
RouteMatchInterface $route_match,
EntityTypeManagerInterface $entity_type_manager,
- AliasManagerInterface $pathAliasManager) {
+ AliasManagerInterface $pathAliasManager,
+ LoggerInterface $logger) {
$this->citationHelper = $citationHelper;
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
$this->pathAliasManager = $pathAliasManager;
+ $this->logger = $logger;
}
/**
@@ -69,7 +82,8 @@ public static function create(ContainerInterface $container) {
$container->get('islandora_citations.helper'),
$container->get('current_route_match'),
$container->get('entity_type.manager'),
- $container->get('path_alias.manager')
+ $container->get('path_alias.manager'),
+ $container->get('logger.factory')->get('islandora_citations')
);
}
@@ -104,6 +118,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
+ //
.
+ // 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, '')) {
+ // 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,
@@ -154,13 +190,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();
+ }
}
/**
diff --git a/src/IslandoraCitationsHelper.php b/src/IslandoraCitationsHelper.php
index 61fa6fe..3da87d1 100644
--- a/src/IslandoraCitationsHelper.php
+++ b/src/IslandoraCitationsHelper.php
@@ -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());
+ }
}
}
diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php
index b2d7df4..78ebd69 100644
--- a/src/Plugin/Block/DisplayCitationsBlock.php
+++ b/src/Plugin/Block/DisplayCitationsBlock.php
@@ -35,6 +35,7 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
+
/**
* Citation helper service.
*
@@ -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;
+ }
}
/**