Skip to content

Commit

Permalink
Merge branch 'main' into dgir-124
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd authored Dec 18, 2023
2 parents 541845d + b2914c7 commit ad1db8a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
58 changes: 50 additions & 8 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
*
Expand All @@ -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;
}

/**
Expand All @@ -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')
);
}

Expand Down Expand Up @@ -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
// <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 @@ -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();
}
}

/**
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

0 comments on commit ad1db8a

Please sign in to comment.