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

Default Value for field_csl_type #52

Merged
merged 1 commit into from
Nov 24, 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
47 changes: 34 additions & 13 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class SelectCslForm extends FormBase {
* @var Drupal\islandora_citations\IslandoraCitationsHelper
*/
protected $citationHelper;
/**
* CSL type value from block.
*
* @var string
*/
private $blockCSLType;
/**
* The route match.
*
Expand Down Expand Up @@ -65,6 +71,8 @@ public function getFormId() {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$block_storage = $this->entityTypeManager->getStorage('block');
// Check if the value is set in block, newly added field.
// Pass it in renderCitation.
$blocks = $block_storage->loadMultiple();
$cslItems = $this->citationHelper->getCitationEntityList();
$default_csl = array_values($cslItems)[0];
Expand All @@ -73,6 +81,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
if (isset($settings['id'])) {
if ($settings['id'] == 'islandora_citations_display_citations') {
$default_csl = !empty($settings['default_csl']) ? $settings['default_csl'] : array_values($cslItems)[0];
$this->blockCSLType = $settings['default_csl_type'];
}
}
}
Expand All @@ -87,7 +96,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#empty_option' => $this->t('- Select csl -'),
'#default_value' => $default_csl,
'#ajax' => [
'callback' => '::renderCitation',
'callback' => '::renderAjaxCitation',
'wrapper' => 'formatted-citation',
'method' => 'html',
'event' => 'change',
Expand All @@ -109,20 +118,15 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* Render CSL response on ajax call.
*/
public function renderCitation(array $form, FormStateInterface $form_state) {
public function renderAjaxCitation(array $form, FormStateInterface $form_state) {
$csl_name = $form_state->getValue('csl_list');
if ($csl_name == '') {
return [
'#children' => '',
];
}
$entity = $this->routeMatch->getParameter('node');
$citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity);

$style = $this->citationHelper->loadStyle($csl_name);

$rendered = $this->citationHelper->renderWithCiteproc($citationItems, $style);

// Method call to render citation.
$rendered = $this->renderCitation($csl_name);
$response = [
'#children' => $rendered['data'],
];
Expand All @@ -143,19 +147,36 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
* Block default csl name.
*/
public function getDefaultCitation($csl_name) {
$entity = $this->routeMatch->getParameter('node');
if (empty($csl_name)) {
return $this->t('Select CSL');
}
try {
$citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity);
$style = $this->citationHelper->loadStyle($csl_name);
$rendered = $this->citationHelper->renderWithCiteproc($citationItems, $style);
// Method call to render citation.
$rendered = $this->renderCitation($csl_name);
return $rendered['data'];
}
catch (\Throwable $e) {
return $e->getMessage();
}
}

/**
* Get rendered data.
*
* @param string $csl_name
* Block default csl name.
*
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
private function renderCitation($csl_name): ?array {
$entity = $this->routeMatch->getParameter('node');
$citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity);
$blockCSLType = $this->blockCSLType;
if (!isset($citationItems[0]->type)) {
$citationItems[0]->type = $blockCSLType;
}
$style = $this->citationHelper->loadStyle($csl_name);
return $this->citationHelper->renderWithCiteproc($citationItems, $style);
}

}
14 changes: 5 additions & 9 deletions src/IslandoraCitationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function loadCslJsonSchema() {
/**
* Cet citations styles from config entity.
*/
public function getCitationEntityList() {
public function getCitationEntityList(): array {
$citationIds = $this->citationsStorage->getQuery()->execute();
$citationEntities = $this->citationsStorage->loadMultiple($citationIds);
$citationList = [];
Expand All @@ -94,7 +94,7 @@ public function getCitationEntityList() {
/**
* Load styles from citations style language.
*/
public function getAllCslsFromCiteproc() {
public function getAllCslsFromCiteproc(): array {
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot(DRUPAL_ROOT);
$vendorDir = $drupalFinder->getVendorDir();
Expand All @@ -110,6 +110,8 @@ public function getAllCslsFromCiteproc() {

/**
* Load style string from entity or file.
*
* @throws \Seboettg\CiteProc\Exception\CiteProcException
*/
public function loadStyle($styleName, $styleType = 'entity') {
if ($styleType == 'entity') {
Expand All @@ -125,8 +127,6 @@ public function loadStyle($styleName, $styleType = 'entity') {

/**
* Render entity with citeproc.
*
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
public function renderWithCiteproc(array $data, string $style, string $mode = 'bibliography') {
try {
Expand All @@ -146,12 +146,8 @@ public function renderWithCiteproc(array $data, string $style, string $mode = 'b
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
* @throws \Exception
*/
public function encodeEntityForCiteproc(EntityInterface $entity) {
public function encodeEntityForCiteproc(EntityInterface $entity): object {
$cslEncodedData = $this->serializer->normalize($entity, 'csl-json');
if (!isset($cslEncodedData['type'])) {
$this->logger->error('CSL encoding error. Type is a mandatory field.');
throw new \Exception('CSL encoding error. Type is a mandatory field.');
}
return (object) $cslEncodedData;
}

Expand Down
29 changes: 28 additions & 1 deletion src/Plugin/Block/DisplayCitationsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\islandora_citations\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Expand All @@ -21,6 +22,13 @@
*/
class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginInterface {

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

/**
* The form builder.
*
Expand All @@ -43,13 +51,16 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The form builder instance.
* @param \Drupal\islandora_citations\IslandoraCitationsHelper $citationHelper
* The CitationHelper instance.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->formBuilder = $formBuilder;
$this->citationHelper = $citationHelper;
}
Expand All @@ -62,6 +73,7 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('form_builder'),
$container->get('islandora_citations.helper')
);
Expand All @@ -83,6 +95,7 @@ public function build() {
public function defaultConfiguration() {
return [
'default_csl' => '',
'default_csl_type' => 'Webpage',
];
}

Expand All @@ -102,6 +115,7 @@ public function blockForm($form, FormStateInterface $form_state) {
else {
$config = $this->getConfiguration();
$defaultCSL = $config['default_csl'];
$defaultCSLType = $config['default_csl_type'];
$form['csl_list'] = [
'#type' => 'select',
'#title' => $this->t('Select default CSL'),
Expand All @@ -110,6 +124,18 @@ public function blockForm($form, FormStateInterface $form_state) {
'#attributes' => ['aria-label' => $this->t('Select CSL')],
'#default_value' => $defaultCSL,
];
$cslTypesVocab = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree('csl_type');
foreach ($cslTypesVocab as $vocab) {
$cslTypesNames[$vocab->name] = $vocab->name;
}
$form['field_csl_type'] = [
'#type' => 'select',
'#title' => $this->t('Object Type (Citation)'),
'#options' => $cslTypesNames,
'#empty_option' => $this->t('- Select Citation -'),
'#attributes' => ['aria-label' => $this->t('Select Citation')],
'#default_value' => $defaultCSLType,
];
}

return $form;
Expand All @@ -121,6 +147,7 @@ public function blockForm($form, FormStateInterface $form_state) {
public function blockSubmit($form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$this->configuration['default_csl'] = $values['csl_list'];
$this->configuration['default_csl_type'] = $values['field_csl_type'];
}

}