Skip to content

Commit

Permalink
Merge branch '4.x' into tv4g1-issue1843-publishing_cardinality
Browse files Browse the repository at this point in the history
  • Loading branch information
laceysanderson authored Jun 17, 2024
2 parents 59aa131 + 75ab06d commit 1a26d56
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 1 deletion.
28 changes: 28 additions & 0 deletions tripal/src/Entity/TripalEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* "add" = "Drupal\tripal\Form\TripalEntityForm",
* "edit" = "Drupal\tripal\Form\TripalEntityForm",
* "delete" = "Drupal\tripal\Form\TripalEntityDeleteForm",
* "unpublish" = "Drupal\tripal\Form\TripalEntityUnpublishForm",
* },
* "access" = "Drupal\tripal\Access\TripalEntityAccessControlHandler",
* "route_provider" = {
Expand All @@ -52,6 +53,7 @@
* "add-form" = "/bio_data/add/{tripal_entity_type}",
* "edit-form" = "/bio_data/{tripal_entity}/edit",
* "delete-form" = "/bio_data/{tripal_entity}/delete",
* "unpublish-form" = "/bio_data/{tripal_entity}/unpublish",
* "collection" = "/admin/content/bio_data",
* },
* bundle_entity_type = "tripal_entity_type",
Expand Down Expand Up @@ -668,4 +670,30 @@ public function validate() {

return $violations;
}

/**
* Performs a removal of the entity from Drupal.
*
* This function copies the code from the parent::delete() function. It
* does not remove the record from the storage backend. The
* postDelete() function will be triggered.
*/
public function unpublish() {
parent::delete();
}

/**
* Performs a total remove of the record from Drupal and the DB backend.
*
* This function copies the code from the parent::delete() function but
* then performs extra steps to delete the record in the database backend.
* The postDelete() function will also be triggered because it uses the
* parent::delete() function to delete the entity from Drupal.
*/
public function delete() {
parent::delete();
// @todo: add in code to remove entity from the database backend.
}


}
23 changes: 23 additions & 0 deletions tripal/src/Form/TripalEntityDeleteForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Drupal\tripal\Form;

use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;


/**
* Provides a form for deleting Tripal Content entities.
Expand All @@ -11,5 +13,26 @@
*/
class TripalEntityDeleteForm extends ContentEntityDeleteForm {

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to unpublish and permanenty '
. 'delete %name?', ['%name' => $this->entity->label()]);
}

/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}

/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This action is not fully implemented in Tripal v4. It currently functions as an"unpublish".');
}

}
39 changes: 39 additions & 0 deletions tripal/src/Form/TripalEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Component\Serialization\Json;

/**
* Form controller for Tripal Content edit forms.
Expand Down Expand Up @@ -57,4 +58,42 @@ public function save(array $form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.tripal_entity.canonical', ['tripal_entity' => $entity->id()]);
}

/**
*
* {@inheritDoc}
* @see \Drupal\Core\Entity\EntityForm::actions()
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('unpublish-form')) {
$route_info = $this->entity->toUrl('canonical');
$actions['cancel'] = [
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#access' => $this->entity->access('administer tripal content'),
'#attributes' => [
'class' => ['button'],
],
'#url' => $route_info,
];
$route_info = $this->entity->toUrl('unpublish-form');
$actions['unpublish'] = [
'#type' => 'link',
'#title' => $this->t('Unpublish'),
'#access' => $this->entity->access('administer tripal content'),
'#attributes' => [
'class' => ['button', 'button--danger', 'use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 880,
]),
],
'#url' => $route_info,
'#attached' => [
'library' => ['core/drupal.dialog.ajax'],
],
];
}
return $actions;
}
}
82 changes: 82 additions & 0 deletions tripal/src/Form/TripalEntityUnpublishForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Drupal\tripal\Form;

use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides a form for unpublishing Tripal Content entities.
*
* This is equilvalent to deleting an Drupal entity, but in the base of
* Tripal entities, it does not try to delete the item in the database
* back-end.
*
* @ingroup tripal
*/
class TripalEntityUnpublishForm extends ContentEntityDeleteForm {

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to unpublish %name?',
['%name' => $this->entity->label()]);
}

/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Unpublish');
}

/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Unpublishing will remove this page from the site but the data '
. 'will be retained in the database and you can republish it again later if desired.');
}

/**
* Similar to the parent::getDeleteMessage() but custom for unpublishing.
*/
protected function getUnpublishMessage() {
$entity = $this->getEntity();
return $this->t('The @entity-type %label has been unpublished.', [
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
'%label' => $entity->label() ?? $entity->id(),
]);
}

/**
* Similar to the parent::logUnpublishMessage() but custom for unpublishing.
*/
protected function logUnpublishMessage() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
$this->logger($entity->getEntityType()->getProvider())->info('The @entity-type %label has been unpublished.', [
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
'%label' => $entity->label(),
]);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\tripal\Entity\TripalEntity $entity */
$entity = $this->getEntity();
$message = $this->getUnpublishMessage();

$entity->unpublish();
$form_state->setRedirectUrl($this->getRedirectUrl());

$this->messenger()->addStatus($message);
$this->logUnpublishMessage();
}



}
24 changes: 24 additions & 0 deletions tripal/src/ListBuilders/TripalEntityListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Xss;

/**
Expand Down Expand Up @@ -89,6 +90,29 @@ public function load() {
}
}

/**
* {@inheritDoc}
* @see \Drupal\Core\Entity\EntityListBuilder::getDefaultOperations()
*/
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);

if ($entity->access('unpublish') && $entity->hasLinkTemplate('unpublish-form')) {
$operations['unpublish'] = [
'title' => $this->t('Unpublish'),
'weight' => 59,
'attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 880,
]),
],
'url' => $this->ensureDestination($entity->toUrl('unpublish-form')),
];
}

return $operations;
}

}
33 changes: 33 additions & 0 deletions tripal/src/Routing/TripalEntityHtmlRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,43 @@ public function getRoutes(EntityTypeInterface $entity_type) {
if ($collection_route = $this->getCollectionRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.collection", $collection_route);
}
if ($unpublish_route = $this->getUnpublishRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.unpublish", $unpublish_route);
}

return $collection;
}

/**
* Gets the unpublish route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getUnpublishRoute(EntityTypeInterface $entity_type) {

if ($entity_type->hasLinkTemplate('unpublish-form')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('unpublish-form'));
$route->addDefaults([
'_entity_form' => "{$entity_type_id}.unpublish",
'_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::deleteTitle',
])->setRequirement('_entity_access', "{$entity_type_id}.unpublish")
->setOption('parameters', [$entity_type_id => ['type' => 'entity:' . $entity_type_id],
]);

// Entity types with serial IDs can specify this in their route
// requirements, improving the matching process.
if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route->setRequirement($entity_type_id, '\d+');
}
return $route;
}
}

/**
* Gets the collection route.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function testTripalContentPages() {
'entity-add-form' => 'bio_data/add/' . $content_type,
'entity-edit-form' => 'bio_data/' . $entity_id . '/edit',
'entity-delete-form' => 'bio_data/' . $entity_id . '/delete',
'entity-unpublish-form' => 'bio_data/' . $entity_id . '/unpublish',
'entity-collection' => 'admin/content/bio_data',
//'publish-content' => '',
'unpublish-content' => 'admin/content/bio_data/unpublish',
Expand All @@ -294,7 +295,10 @@ public function testTripalContentPages() {
'edit tripal content entities' => ['entity-edit-form'],
'delete tripal content entities' => ['entity-delete-form'],
'view tripal content entities' => ['entity-canonical'],
'administer tripal content' => ['entity-canonical', 'entity-add-page', 'entity-add-form', 'entity-edit-form', 'entity-delete-form', 'entity-collection', 'publish-content', 'unpublish-content'],
'administer tripal content' => ['entity-canonical', 'entity-add-page',
'entity-add-form', 'entity-edit-form', 'entity-delete-form',
'entity-collection', 'publish-content', 'unpublish-content',
'entity-unpublish-form'],
'manage tripal content types' => ['entitytype-add-form', 'entitytype-edit-form', 'entitytype-delete-form', 'entitytype-collection'],
'administer tripal_entity fields' => ['entitytype-manage-fields'],
'administer tripal_entity form display' => ['entitytype-manage-form'],
Expand Down
7 changes: 7 additions & 0 deletions tripal/tripal.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ entity.tripal_entity.delete_form:
route_name: entity.tripal_entity.delete_form
base_route: entity.tripal_entity.canonical
title: Delete
weight: 20

# -- Provides an Unpublish task link on each Tripal content page.
entity.tripal_entity.unpublish_form:
route_name: entity.tripal_entity.unpublish_form
base_route: entity.tripal_entity.canonical
title: Unpublish
weight: 10

##
Expand Down
13 changes: 13 additions & 0 deletions tripal/tripal.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ tripal.content_type_field_check:
requirements:
_permission: 'administer tripal'

# Adds an unpublished link to Tripal entity pages.
entity.tripal_entity.unpublish_form:
path: 'bio_data/{tripal_entity}/unpublish'
defaults:
_form: '\Drupal\tripal\Form\TripalEntityUnpublishForm'
_title: 'Unpublish'
options:
parameters:
tripal_entity:
type: entity:tripal_entity
requirements:
_permission: 'administer tripal content'

##
# Controlled Vocabularies.
##
Expand Down

0 comments on commit 1a26d56

Please sign in to comment.