Skip to content

Commit

Permalink
NBBIB-487 Add form components
Browse files Browse the repository at this point in the history
  • Loading branch information
camilocodes committed Jan 17, 2025
1 parent d760ac3 commit edc2487
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 0 deletions.
15 changes: 15 additions & 0 deletions custom/modules/yabrm/src/Form/ConferenceReferenceDeleteForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Drupal\yabrm\Form;

use Drupal\Core\Entity\ContentEntityDeleteForm;

/**
* Provides a form for deleting Conference reference entities.
*
* @ingroup yabrm
*/
class ConferenceReferenceDeleteForm extends ContentEntityDeleteForm {


}
104 changes: 104 additions & 0 deletions custom/modules/yabrm/src/Form/ConferenceReferenceForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Drupal\yabrm\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxy;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Form controller for Conference reference edit forms.
*
* @ingroup yabrm
*/
class ConferenceReferenceForm extends ContentEntityForm {

/**
* For services dependency injection.
*
* @var Drupal\Core\Session\AccountProxy
*/
protected $currentUser;

/**
* Class constructor.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time_interface
* The time service.
* @param Drupal\Core\Session\AccountProxy $current_user
* For services dependency injection.
*/
public function __construct(
EntityRepositoryInterface $entity_repository,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time_interface,
AccountProxy $current_user) {
parent::__construct(
$entity_repository,
$entity_type_bundle_info,
$time_interface
);
$this->currentUser = $current_user;
}

/**
* Object create method.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* Container interface.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user')
);
}

/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;

// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('revision') && $form_state->getValue('revision') != FALSE) {
$entity->setNewRevision();

// If a new revision is created, save the current user as revision author.
$entity->setRevisionCreationTime($this->time->getRequestTime());
$entity->setRevisionUserId($this->currentUser->id());
}
else {
$entity->setNewRevision(FALSE);
}

$status = parent::save($form, $form_state);

switch ($status) {
case SAVED_NEW:
$this->messenger()->addMessage($this->t('Created the %label Conference reference.', [
'%label' => $entity->label(),
]));
break;

default:
$this->messenger()->addMessage($this->t('Saved the %label Conference reference.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.yabrm_conference.canonical', ['yabrm_conference' => $entity->id()]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Drupal\yabrm\Form;

use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a form for deleting a Conference reference revision.
*
* @ingroup yabrm
*/
class ConferenceReferenceRevisionDeleteForm extends ConfirmFormBase {


/**
* The Conference reference revision.
*
* @var \Drupal\yabrm\Entity\ConferenceReferenceInterface
*/
protected $revision;

/**
* The Conference reference storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $conferenceReferenceStorage;

/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;

/**
* Constructs a new ConferenceReferenceRevisionDeleteForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(
EntityStorageInterface $entity_storage,
Connection $connection,
DateFormatterInterface $date_formatter) {
$this->conferenceReferenceStorage = $entity_storage;
$this->connection = $connection;
$this->dateFormatter = $date_formatter;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity_type.manager');
return new static(
$entity_manager->getStorage('yabrm_conference'),
$container->get('database'),
$container->get('date.formatter')
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'yabrm_conference_revision_delete_confirm';
}

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.yabrm_conference.version_history', ['yabrm_conference' => $this->revision->id()]);
}

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

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $yabrm_conference_revision = NULL) {
$this->revision = $this->conferenceReferenceStorage->loadRevision($yabrm_conference_revision);
$form = parent::buildForm($form, $form_state);

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->conferenceReferenceStorage->deleteRevision($this->revision->getRevisionId());

$this->logger('content')->notice('Conference reference: deleted %title revision %revision.', [
'%title' => $this->revision->label(),
'%revision' => $this->revision->getRevisionId(),
]);
$this->messenger()->addMessage($this->t('Revision from %revision-date of Conference reference %title has been deleted.', [
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
'%title' => $this->revision->label(),
]));
$form_state->setRedirect(
'entity.yabrm_conference.canonical',
['yabrm_conference' => $this->revision->id()]
);
if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {yabrm_conference_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) {
$form_state->setRedirect(
'entity.yabrm_conference.version_history',
['yabrm_conference' => $this->revision->id()]
);
}
}

}
Loading

0 comments on commit edc2487

Please sign in to comment.