Skip to content

Commit

Permalink
Merge pull request #398 from esmero/ISSUE-397
Browse files Browse the repository at this point in the history
ISSUE-397: Adds Usage Tab to Metadata Display Entities
  • Loading branch information
alliomeria authored Jan 4, 2024
2 parents b93dfdf + 8749187 commit 3e1d052
Show file tree
Hide file tree
Showing 9 changed files with 553 additions and 3 deletions.
6 changes: 6 additions & 0 deletions format_strawberryfield.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ metadatadisplay.delete_confirm:
title: Delete
weight: 10

metadatadisplay.usage_tab:
route_name: entity.metadatadisplay_entity.usage_form
base_route: entity.metadatadisplay_entity.canonical
title: Usage
weight: 11

metadatadisplay.admin:
title: Metadata Display
route_name: entity.metadatadisplay_entity.collection
Expand Down
15 changes: 15 additions & 0 deletions format_strawberryfield.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ entity.metadatadisplay_entity.delete_form:
requirements:
_entity_access: 'metadatadisplay_entity.delete'

entity.metadatadisplay_entity.usage_form:
path: '/metadatadisplay/{metadatadisplay_entity}/usage'
defaults:
_form: '\Drupal\format_strawberryfield\Form\MetadataDisplayUsageForm'
_title: 'Usage'
requirements:
_permission: 'administer metadatadisplay entity'
_entity_access: 'metadatadisplay_entity.edit'
options:
parameters:
metadatadisplay_entity:
type: 'entity:metadatadisplay_entity'
_admin_route: TRUE


# Metadatadisplay settings route

format_strawberryfield.metadatadisplay_settings:
Expand Down
3 changes: 3 additions & 0 deletions format_strawberryfield.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@entity_type.manager']
format_strawberryfield.metadatadisplay_usage_service:
class: Drupal\format_strawberryfield\MetadataDisplayUsageService
arguments: [ '@entity.repository', '@entity_type.manager', '@config.factory', '@entity_display.repository', '@current_user', '@module_handler' ]
6 changes: 5 additions & 1 deletion src/Entity/Controller/MetadataDisplayListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public function render() {
*/
public function buildHeader() {
$header['id'] = $this->t('Metadata Display ID');
$header['uuid'] = $this->t('UUID');
$header['name'] = $this->t('Name');
$header['last update'] = $this->t('Last update');
$header['In use'] = $this->t('In use');
return $header + parent::buildHeader();
}

Expand All @@ -52,9 +54,11 @@ public function buildHeader() {
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\format_strawberryfield\Entity\MetadataDisplayEntity */
$row['id'] = $entity->id();
$row['uuid'] = $entity->uuid();
$row['name'] = $entity->toLink();
$row['last update'] = \Drupal::service('date.formatter')->format($entity->changed->value, 'custom', 'd/m/Y');
$row['In use'] = \Drupal::service('format_strawberryfield.metadatadisplay_usage_service')->getUsage($entity) ? 'YES': 'NO';
return $row + parent::buildRow($entity);
}

}
}
2 changes: 1 addition & 1 deletion src/Entity/MetadataDisplayEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
* "canonical" = "/metadatadisplay/{metadatadisplay_entity}",
* "edit-form" = "/metadatadisplay/{metadatadisplay_entity}/edit",
* "delete-form" = "/metadatadisplay/{metadatadisplay_entity}/delete",
* "usage-form" = "/metadatadisplay/{metadatadisplay_entity}/usage",
* "collection" = "/metadatadisplay/list"
* },
* field_ui_base_route = "format_strawberryfield.metadatadisplay_settings",
Expand Down Expand Up @@ -186,7 +187,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
// Calculate RelatedCacheTags.
$this->invalidateTempMetadataDisplayCaches();
$this->getRelatedCacheTagsToInvalidate(TRUE);

}

/**
Expand Down
80 changes: 80 additions & 0 deletions src/Form/MetadataDisplayUsageForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Drupal\format_strawberryfield\Form;
use Drupal\ami\Entity\amiSetEntity;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\format_strawberryfield\MetadataDisplayInterface;
use Drupal\format_strawberryfield\MetadataDisplayUsageService;
use Drupal\format_strawberryfield\MetadataDisplayUsageServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;

/**
* Form controller for the MetadataDisplayEntity entity Usage forms.
*
* @ingroup format_strawberryfield
*/
class MetadataDisplayUsageForm extends FormBase {
private MetadataDisplayUsageServiceInterface $metadatadisplayUsageService;

/**
* @param MetadataDisplayUsageServiceInterface $metadatadisplay_usage_service
*/
public function __construct(MetadataDisplayUsageServiceInterface $metadatadisplay_usage_service) {
$this->metadatadisplayUsageService = $metadatadisplay_usage_service;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('format_strawberryfield.metadatadisplay_usage_service'),
);
}

/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'format_strawberryfield_metadatadisplay_usage';
}

/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Empty implementation of the abstract submit class.
}


/**
* Define the form used for MetadataDisplayEntity settings.
* @return array
* Form definition array.
*
* @param array $form
* An associative array containing the structure of the form.
* @param FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
public function buildForm(array $form, FormStateInterface $form_state, MetadataDisplayInterface $metadatadisplay_entity = NULL) {
if ($metadatadisplay_entity) {
$form = $this->metadatadisplayUsageService->getRenderableUsage($metadatadisplay_entity);
}
return $form;
}
}
2 changes: 1 addition & 1 deletion src/Form/ViewModeMappingSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];
}
else {
// The View Mode saved in our settings is not longer available, show a select box
// The View Mode saved in our settings is no longer available, show a select box
$form['table-row'][$key]['vm'] = [
'#prefix' => '<div>' . $view_mode_mapping_for_this_row . '</div>',
'#type' => 'select',
Expand Down
Loading

0 comments on commit 3e1d052

Please sign in to comment.