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

Fixes #3712 Add Author Role to Publications #4075

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
"drupal/imagemagick": {
"Imagemagick fails to convert animated gifs into animated webps (3295470)": "https://www.drupal.org/files/issues/2023-08-29/imagemagick-convertation-keep-animation-3295470-10.patch"
},
"drupal/inline_entity_form": {
"Support Referencing the same entity multiple times (3363988)": "https://git.drupalcode.org/project/inline_entity_form/-/merge_requests/119.diff"
},
"drupal/media_migration": {
"Catch migrate RequirementsExceptions (3268480)": "https://www.drupal.org/files/issues/2022-03-08/3268480-2.patch"
},
Expand Down
212 changes: 212 additions & 0 deletions modules/custom/az_publication/az_publication.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\az_publication\Plugin\views\argument\AZCitationStyleArgument;
use Drupal\filter\Entity\FilterFormat;
Expand Down Expand Up @@ -110,6 +111,9 @@ function az_publication_theme_suggestions_block_alter(array &$suggestions, array
*/
function az_publication_theme() {
return [
'az_inline_entity_role_form_entity_table' => [
'render element' => 'form',
],
'block__views_block__az_publications_az_author_person' => [
'template' => 'block--views-block--az-publications-az-author-person',
'base hook' => 'block',
Expand Down Expand Up @@ -255,6 +259,39 @@ function az_publication_node_view(array &$build, EntityInterface $entity, Entity
}
}
}
// @todo Don't duplicate author code.
if (!empty($entity->field_az_contributors)) {
foreach ($entity->field_az_contributors as $item) {
if (!empty($item->entity)) {
$author = new stdClass();
$author_entity = $repository->getTranslationFromContext($item->entity);
// Only literal name.
if (!empty($author_entity->field_az_author_literal->value) && empty($author_entity->field_az_author_parse->value)) {
$author->family = trim($author_entity->field_az_author_literal->value);
}
else {
// Map regular string fields.
foreach ($amap as $asource => $adestination) {
if (!empty($author_entity->{$asource}->value)) {
$author->{$adestination} = trim($author_entity->{$asource}->value);
}
}
}
// Special field due to being an URL.
if (!empty($author_entity->field_az_author_link->uri)) {
$author->itemlink = $author_entity->field_az_author_link->uri;
}
// Special field handling for person reference.
if (!empty($author_entity->field_az_author_person->entity)) {
$person = $repository->getTranslationFromContext($author_entity->field_az_author_person->entity);
$author->itemlink = $person->toUrl()->toString();
}
// Add to schema based on role.
$role = $item->role ?? 'author';
$blob->{$role}[] = $author;
}
}
}
// Handle publication date if available.
if (!empty($entity->field_az_publication_date->value)) {
$blob->issued = _az_publication_publication_date_object($entity);
Expand Down Expand Up @@ -691,6 +728,181 @@ function az_publication_form_node_az_publication_edit_form_alter(&$form, FormSta
_az_publication_form_revisions($form, $form_state, $form_id);
}

/**
* A callback to return the role property form element.
*
* This callback returns the role property form element for the
* inline entity form subclass.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The current entity row of the inline entity form.
* @param array $variables
* Callback arguments from inline entity form theme function.
*
* @return mixed
* Form element render array or translatable markup.
*/
function az_publication_inline_entity_label_callback(EntityInterface $entity, $variables) {

foreach (Element::children($variables['form']) as $key) {
if (!empty($variables['form'][$key]['role']) && ($entity === $variables['form'][$key]['#entity'])) {
return $variables['form'][$key]['role'];
}
}

// Couldn't find our entity.
// @todo make this not require entity comparison.
return t('No role');
}

/**
* Prepares variables for az_inline_entity_role_form_entity_table templates.
*
* Default template: az-inline-entity-role-form-entity-table.html.twig.
*
* @param array $variables
* An associative array containing:
* - form: A render element representing the form.
*/
function template_preprocess_az_inline_entity_role_form_entity_table(array &$variables) {
$form = $variables['form'];
$entity_type = $form['#entity_type'];

$fields = $form['#table_fields'];
$has_tabledrag = \Drupal::entityTypeManager()->getHandler($entity_type, 'inline_form')->isTableDragEnabled($form);

// Sort the fields by weight.
uasort($fields, '\Drupal\Component\Utility\SortArray::sortByWeightElement');

$header = [];
if ($has_tabledrag) {
$header[] = ['data' => '', 'class' => ['ief-tabledrag-header']];
$header[] = [
'data' => t('Sort order'),
'class' => ['ief-sort-order-header'],
];
}
// Add header columns for each field.
$first = TRUE;
foreach ($fields as $field_name => $field) {
$column = [
'data' => $field['label'],
'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name],
];
// The first column gets a special class.
if ($first) {
$column['class'][] = 'ief-first-column-header';
$first = FALSE;
}
$header[] = $column;
}
$header[] = t('Role');
$header[] = t('Operations');

// Build an array of entity rows for the table.
$rows = [];
foreach (Element::children($form) as $key) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $form[$key]['#entity'];
$row_classes = ['ief-row-entity'];
$cells = [];
if ($has_tabledrag) {
$cells[] = [
'data' => ['#plain_text' => ''],
'#wrapper_attributes' => ['class' => ['ief-tabledrag-handle']],
];
$cells[] = ['data' => $form[$key]['delta']];
$row_classes[] = 'draggable';
}
// Add a special class to rows that have a form underneath, to allow
// for additional styling.
if (!empty($form[$key]['form'])) {
$row_classes[] = 'ief-row-entity-form';
}

foreach ($fields as $field_name => $field) {
if ($field['type'] === 'label') {
$data = ['#markup' => $variables['form'][$key]['#label']];
}
elseif ($field['type'] === 'field' && $entity->hasField($field_name)) {
$display_options = ['label' => 'hidden'];
if (isset($field['display_options'])) {
$display_options += $field['display_options'];
}
$data = $entity->get($field_name)->view($display_options);
}
elseif ($field['type'] === 'callback') {
$arguments = [
'entity' => $entity,
'variables' => $variables,
];
if (isset($field['callback_arguments'])) {
$arguments = array_merge($arguments, $field['callback_arguments']);
}

$data = call_user_func_array($field['callback'], array_values($arguments));

// Backward compatibility for callbacks that just provide a string not
// an array.
if (!is_array($data)) {
$data = ['#markup' => $data];
}
}
else {
$data = ['#markup' => t('N/A')];
}

$cells[$field_name] = array_merge($data, ['#wrapper_attributes' => ['class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]]]);
}

// Display the role field from the form.
$cells['role'] = $form[$key]['role'] ?? ['#markup' => t('N/A')];

// Add the buttons belonging to the "Operations" column, when entity is not
// being displayed as a form.
if (empty($form[$key]['form'])) {
$cells['actions'] = $form[$key]['actions'];
}
// Create the row.
$rows[] = $cells + ['#attributes' => ['class' => $row_classes]];
// If the current entity array specifies a form, output it in the next row.
if (!empty($form[$key]['form'])) {
$row = [];
$row[] = $form[$key]['form'] + ['#wrapper_attributes' => ['colspan' => count($fields) + 1]];
$rows[] = $row + [
'#attributes' =>
[
'class' => ['ief-row-form'],
'no_striping' => TRUE,
],
];
}
}

if (!empty($rows)) {
$tabledrag = [];
if ($has_tabledrag) {
$tabledrag = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'ief-entity-delta',
],
];
}

$variables['table'] = [
'#type' => 'table',
'#header' => $header,
'#attributes' => [
'id' => 'ief-entity-table-' . $form['#id'],
'class' => ['ief-entity-table'],
],
'#tabledrag' => $tabledrag,
] + $rows;
}
}

/**
* Implements hook_views_data_alter().
*
Expand Down
Loading