Commit 1c4af85a authored by webchick's avatar webchick

Issue #2060865 by benjy, larowlan, tim.plunkett: Modernize custom_block.module forms.

parent b2a6ece3
...@@ -7,15 +7,61 @@ ...@@ -7,15 +7,61 @@
namespace Drupal\custom_block; namespace Drupal\custom_block;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityFormController; use Drupal\Core\Entity\ContentEntityFormController;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Form controller for the custom block edit forms. * Form controller for the custom block edit forms.
*/ */
class CustomBlockFormController extends ContentEntityFormController { class CustomBlockFormController extends ContentEntityFormController {
/**
* The custom block storage.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $customBlockStorage;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Constructs a CustomBlockFormController object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $custom_block_storage
* The custom block storage controller.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
*/
public function __construct(EntityManagerInterface $entity_manager, EntityStorageControllerInterface $custom_block_storage, LanguageManager $language_manager) {
parent::__construct($entity_manager);
$this->customBlockStorage = $custom_block_storage;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity.manager');
return new static(
$entity_manager,
$entity_manager->getStorageController('custom_block'),
$container->get('language_manager')
);
}
/** /**
* Overrides \Drupal\Core\Entity\EntityFormController::prepareEntity(). * Overrides \Drupal\Core\Entity\EntityFormController::prepareEntity().
* *
...@@ -36,7 +82,7 @@ protected function prepareEntity() { ...@@ -36,7 +82,7 @@ protected function prepareEntity() {
} }
/** /**
* Overrides \Drupal\Core\Entity\EntityFormController::form(). * {@inheritdoc}
*/ */
public function form(array $form, array &$form_state) { public function form(array $form, array &$form_state) {
$block = $this->entity; $block = $this->entity;
...@@ -53,23 +99,25 @@ public function form(array $form, array &$form_state) { ...@@ -53,23 +99,25 @@ public function form(array $form, array &$form_state) {
// Basic block information. // Basic block information.
$form['info'] = array( $form['info'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Block description'), '#title' => $this->t('Block description'),
'#required' => TRUE, '#required' => TRUE,
'#default_value' => $block->label(), '#default_value' => $block->label(),
'#weight' => -5, '#weight' => -5,
'#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))), '#description' => $this->t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => $this->url('block.admin_display'))),
); );
$language_configuration = module_invoke('language', 'get_default_configuration', 'custom_block', $block->bundle()); if ($this->moduleHandler->moduleExists('language')) {
$language_configuration = language_get_default_configuration('custom_block', $block->bundle());
// Set the correct default language. // Set the correct default language.
if ($block->isNew() && !empty($language_configuration['langcode'])) { if ($block->isNew()) {
$language_default = language($language_configuration['langcode']); $language_default = $this->languageManager->getLanguage($language_configuration['langcode']);
$block->langcode->value = $language_default->id; $block->langcode->value = $language_default->id;
}
} }
$form['langcode'] = array( $form['langcode'] = array(
'#title' => t('Language'), '#title' => $this->t('Language'),
'#type' => 'language_select', '#type' => 'language_select',
'#default_value' => $block->getUntranslated()->language()->id, '#default_value' => $block->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL, '#languages' => Language::STATE_ALL,
...@@ -85,7 +133,7 @@ public function form(array $form, array &$form_state) { ...@@ -85,7 +133,7 @@ public function form(array $form, array &$form_state) {
// current user has the ability to check that option. // current user has the ability to check that option.
$form['revision_information'] = array( $form['revision_information'] = array(
'#type' => 'details', '#type' => 'details',
'#title' => t('Revision information'), '#title' => $this->t('Revision information'),
'#collapsible' => TRUE, '#collapsible' => TRUE,
// Collapsed by default when "Create new revision" is unchecked. // Collapsed by default when "Create new revision" is unchecked.
'#collapsed' => !$block->isNewRevision(), '#collapsed' => !$block->isNewRevision(),
...@@ -102,7 +150,7 @@ public function form(array $form, array &$form_state) { ...@@ -102,7 +150,7 @@ public function form(array $form, array &$form_state) {
$form['revision_information']['revision'] = array( $form['revision_information']['revision'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => t('Create new revision'), '#title' => $this->t('Create new revision'),
'#default_value' => $block->isNewRevision(), '#default_value' => $block->isNewRevision(),
'#access' => $account->hasPermission('administer blocks'), '#access' => $account->hasPermission('administer blocks'),
); );
...@@ -120,10 +168,10 @@ public function form(array $form, array &$form_state) { ...@@ -120,10 +168,10 @@ public function form(array $form, array &$form_state) {
$form['revision_information']['log'] = array( $form['revision_information']['log'] = array(
'#type' => 'textarea', '#type' => 'textarea',
'#title' => t('Revision log message'), '#title' => $this->t('Revision log message'),
'#rows' => 4, '#rows' => 4,
'#default_value' => $block->getRevisionLog(), '#default_value' => $block->getRevisionLog(),
'#description' => t('Briefly describe the changes you have made.'), '#description' => $this->t('Briefly desribe the changes you have made.'),
); );
return parent::form($form, $form_state, $block); return parent::form($form, $form_state, $block);
...@@ -151,7 +199,7 @@ public function submit(array $form, array &$form_state) { ...@@ -151,7 +199,7 @@ public function submit(array $form, array &$form_state) {
} }
/** /**
* Overrides \Drupal\Core\Entity\EntityFormController::save(). * {@inheritdoc}
*/ */
public function save(array $form, array &$form_state) { public function save(array $form, array &$form_state) {
$block = $this->entity; $block = $this->entity;
...@@ -163,11 +211,11 @@ public function save(array $form, array &$form_state) { ...@@ -163,11 +211,11 @@ public function save(array $form, array &$form_state) {
if ($insert) { if ($insert) {
watchdog('content', '@type: added %info.', $watchdog_args, WATCHDOG_NOTICE); watchdog('content', '@type: added %info.', $watchdog_args, WATCHDOG_NOTICE);
drupal_set_message(t('@type %info has been created.', $t_args)); drupal_set_message($this->t('@type %info has been created.', $t_args));
} }
else { else {
watchdog('content', '@type: updated %info.', $watchdog_args, WATCHDOG_NOTICE); watchdog('content', '@type: updated %info.', $watchdog_args, WATCHDOG_NOTICE);
drupal_set_message(t('@type %info has been updated.', $t_args)); drupal_set_message($this->t('@type %info has been updated.', $t_args));
} }
if ($block->id()) { if ($block->id()) {
...@@ -192,20 +240,20 @@ public function save(array $form, array &$form_state) { ...@@ -192,20 +240,20 @@ public function save(array $form, array &$form_state) {
else { else {
// In the unlikely case something went wrong on save, the block will be // In the unlikely case something went wrong on save, the block will be
// rebuilt and block form redisplayed. // rebuilt and block form redisplayed.
drupal_set_message(t('The block could not be saved.'), 'error'); drupal_set_message($this->t('The block could not be saved.'), 'error');
$form_state['rebuild'] = TRUE; $form_state['rebuild'] = TRUE;
} }
// Clear the page and block caches. // Clear the page and block caches.
cache_invalidate_tags(array('content' => TRUE)); Cache::invalidateTags(array('content' => TRUE));
} }
/** /**
* Overrides \Drupal\Core\Entity\EntityFormController::delete(). * {@inheritdoc}
*/ */
public function delete(array $form, array &$form_state) { public function delete(array $form, array &$form_state) {
$destination = array(); $destination = array();
$query = \Drupal::request()->query; $query = $this->getRequest()->query;
if (!is_null($query->get('destination'))) { if (!is_null($query->get('destination'))) {
$destination = drupal_get_destination(); $destination = drupal_get_destination();
$query->remove('destination'); $query->remove('destination');
...@@ -226,12 +274,11 @@ public function delete(array $form, array &$form_state) { ...@@ -226,12 +274,11 @@ public function delete(array $form, array &$form_state) {
*/ */
public function validateForm(array &$form, array &$form_state) { public function validateForm(array &$form, array &$form_state) {
if ($this->entity->isNew()) { if ($this->entity->isNew()) {
// @todo Inject this once https://drupal.org/node/2060865 is in. $exists = $this->customBlockStorage->loadByProperties(array('info' => $form_state['values']['info']));
$exists = \Drupal::entityManager()->getStorageController('custom_block')->loadByProperties(array('info' => $form_state['values']['info']));
if (!empty($exists)) { if (!empty($exists)) {
$this->setFormError('info', $form_state, $this->t('A block with description %name already exists.', array( $this->setFormError('info', $form_state, $this->t('A block with description %name already exists.', array(
'%name' => $form_state['values']['info'] '%name' => $form_state['values']['info'],
))); )));
} }
} }
} }
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\custom_block\Form; namespace Drupal\custom_block\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase; use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Symfony\Component\HttpFoundation\Request;
/** /**
* Provides a confirmation form for deleting a custom block entity. * Provides a confirmation form for deleting a custom block entity.
...@@ -41,8 +40,7 @@ public function getConfirmText() { ...@@ -41,8 +40,7 @@ public function getConfirmText() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, array &$form_state, Request $request = NULL) { public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state, $request);
$instances = $this->entity->getInstances(); $instances = $this->entity->getInstances();
$form['message'] = array( $form['message'] = array(
...@@ -50,7 +48,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU ...@@ -50,7 +48,7 @@ public function buildForm(array $form, array &$form_state, Request $request = NU
'#access' => !empty($instances), '#access' => !empty($instances),
); );
return $form; return parent::buildForm($form, $form_state);
} }
/** /**
......
...@@ -46,7 +46,7 @@ public static function create(ContainerInterface $container) { ...@@ -46,7 +46,7 @@ public static function create(ContainerInterface $container) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getQuestion() { public function getQuestion() {
return t('Are you sure you want to delete %label?', array('%label' => $this->entity->label())); return $this->t('Are you sure you want to delete %label?', array('%label' => $this->entity->label()));
} }
/** /**
...@@ -62,7 +62,7 @@ public function getCancelRoute() { ...@@ -62,7 +62,7 @@ public function getCancelRoute() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getConfirmText() { public function getConfirmText() {
return t('Delete'); return $this->t('Delete');
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment