Commit 73fbcf80 authored by alexpott's avatar alexpott

Issue #2011018 by tim.plunkett: Reconcile entity forms and confirm forms.

parent 025aa2a1
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityConfirmFormBase.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a generic base class for an entity-based confirmation form.
*/
abstract class EntityConfirmFormBase extends EntityFormController implements ConfirmFormInterface {
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Confirm');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return t('Cancel');
}
/**
* {@inheritdoc}
*/
public function getFormName() {
return 'confirm';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$this->request = $request;
$form = parent::buildForm($form, $form_state);
$form['#attributes']['class'][] = 'confirmation';
$form['description'] = array('#markup' => $this->getDescription());
$form[$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
// By default, render the form using theme_confirm_form().
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function init(array &$form_state) {
parent::init($form_state);
drupal_set_title($this->getQuestion(), PASS_THROUGH);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link.
if ($this->request->query->has('destination')) {
$options = drupal_parse_url($this->request->query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
}
else {
$options = array('path' => $path);
}
$actions['cancel'] = array(
'#type' => 'link',
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
return $actions;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityNGConfirmFormBase.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a generic base class for an entity-based confirmation form.
*/
abstract class EntityNGConfirmFormBase extends EntityFormControllerNG implements ConfirmFormInterface {
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Confirm');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return t('Cancel');
}
/**
* {@inheritdoc}
*/
public function getFormName() {
return 'confirm';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$this->request = $request;
$form = parent::buildForm($form, $form_state);
$form['#attributes']['class'][] = 'confirmation';
$form['description'] = array('#markup' => $this->getDescription());
$form[$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
// By default, render the form using theme_confirm_form().
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function init(array &$form_state) {
parent::init($form_state);
drupal_set_title($this->getQuestion(), PASS_THROUGH);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link.
if ($this->request->query->has('destination')) {
$options = drupal_parse_url($this->request->query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
}
else {
$options = array('path' => $path);
}
$actions['cancel'] = array(
'#type' => 'link',
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
return $actions;
}
}
...@@ -7,80 +7,49 @@ ...@@ -7,80 +7,49 @@
namespace Drupal\Core\Form; namespace Drupal\Core\Form;
use Symfony\Component\HttpFoundation\Request;
/** /**
* Provides an generic base class for a confirmation form. * Provides an generic base class for a confirmation form.
*/ */
abstract class ConfirmFormBase implements FormInterface { abstract class ConfirmFormBase implements ConfirmFormInterface {
/**
* Returns the question to ask the user.
*
* @return string
* The form question. The page title will be set to this value.
*/
abstract protected function getQuestion();
/**
* Returns the page to go to if the user cancels the action.
*
* @return string|array
* This can be either:
* - A string containing a Drupal path.
* - An associative array with a 'path' key. Additional array values are
* passed as the $options parameter to l().
* If the 'destination' query parameter is set in the URL when viewing a
* confirmation form, that value will be used instead of this path.
*/
abstract protected function getCancelPath();
/** /**
* Returns additional text to display as a description. * {@inheritdoc}
*
* @return string
* The form description.
*/ */
protected function getDescription() { public function getDescription() {
return t('This action cannot be undone.'); return t('This action cannot be undone.');
} }
/** /**
* Returns a caption for the button that confirms the action. * {@inheritdoc}
*
* @return string
* The form confirmation text.
*/ */
protected function getConfirmText() { public function getConfirmText() {
return t('Confirm'); return t('Confirm');
} }
/** /**
* Returns a caption for the link which cancels the action. * {@inheritdoc}
*
* @return string
* The form cancellation text.
*/ */
protected function getCancelText() { public function getCancelText() {
return t('Cancel'); return t('Cancel');
} }
/** /**
* Returns the internal name used to refer to the confirmation item. * {@inheritdoc}
*
* @return string
* The internal form name.
*/ */
protected function getFormName() { public function getFormName() {
return 'confirm'; return 'confirm';
} }
/** /**
* Implements \Drupal\Core\Form\FormInterface::buildForm(). * {@inheritdoc}
*/ */
public function buildForm(array $form, array &$form_state) { public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$path = $this->getCancelPath(); $path = $this->getCancelPath();
// Prepare cancel link. // Prepare cancel link.
if (isset($_GET['destination'])) { if ($request->query->has('destination')) {
$options = drupal_parse_url($_GET['destination']); $options = drupal_parse_url($request->query->get('destination'));
} }
elseif (is_array($path)) { elseif (is_array($path)) {
$options = $path; $options = $path;
...@@ -114,7 +83,7 @@ public function buildForm(array $form, array &$form_state) { ...@@ -114,7 +83,7 @@ public function buildForm(array $form, array &$form_state) {
} }
/** /**
* Implements \Drupal\Core\Form\FormInterface::validateForm(). * {@inheritdoc}
*/ */
public function validateForm(array &$form, array &$form_state) { public function validateForm(array &$form, array &$form_state) {
} }
......
<?php
/**
* @file
* Contains \Drupal\Core\Form\ConfirmFormInterface.
*/
namespace Drupal\Core\Form;
/**
* Defines the behavior a confirmation form.
*/
interface ConfirmFormInterface extends FormInterface {
/**
* Returns the question to ask the user.
*
* @return string
* The form question. The page title will be set to this value.
*/
public function getQuestion();
/**
* Returns the page to go to if the user cancels the action.
*
* @return string|array
* This can be either:
* - A string containing a Drupal path.
* - An associative array with a 'path' key. Additional array values are
* passed as the $options parameter to l().
* If the 'destination' query parameter is set in the URL when viewing a
* confirmation form, that value will be used instead of this path.
*/
public function getCancelPath();
/**
* Returns additional text to display as a description.
*
* @return string
* The form description.
*/
public function getDescription();
/**
* Returns a caption for the button that confirms the action.
*
* @return string
* The form confirmation text.
*/
public function getConfirmText();
/**
* Returns a caption for the link which cancels the action.
*
* @return string
* The form cancellation text.
*/
public function getCancelText();
/**
* Returns the internal name used to refer to the confirmation item.
*
* @return string
* The internal form name.
*/
public function getFormName();
}
...@@ -73,5 +73,6 @@ function action_menu() { ...@@ -73,5 +73,6 @@ function action_menu() {
function action_entity_info(&$entity_info) { function action_entity_info(&$entity_info) {
$entity_info['action']['controllers']['form']['add'] = 'Drupal\action\ActionAddFormController'; $entity_info['action']['controllers']['form']['add'] = 'Drupal\action\ActionAddFormController';
$entity_info['action']['controllers']['form']['edit'] = 'Drupal\action\ActionEditFormController'; $entity_info['action']['controllers']['form']['edit'] = 'Drupal\action\ActionEditFormController';
$entity_info['action']['controllers']['form']['delete'] = 'Drupal\action\Form\ActionDeleteForm';
$entity_info['action']['controllers']['list'] = 'Drupal\action\ActionListController'; $entity_info['action']['controllers']['list'] = 'Drupal\action\ActionListController';
} }
...@@ -22,7 +22,7 @@ action_admin_configure: ...@@ -22,7 +22,7 @@ action_admin_configure:
action_delete: action_delete:
pattern: 'admin/config/system/actions/configure/{action}/delete' pattern: 'admin/config/system/actions/configure/{action}/delete'
defaults: defaults:
_form: '\Drupal\action\Form\DeleteForm' _entity_form: 'action.delete'
requirements: requirements:
_permission: 'administer actions' _permission: 'administer actions'
...@@ -2,72 +2,47 @@ ...@@ -2,72 +2,47 @@
/** /**
* @file * @file
* Contains \Drupal\action\Form\DeleteForm. * Contains \Drupal\action\Form\ActionDeleteForm.
*/ */
namespace Drupal\action\Form; namespace Drupal\action\Form;
use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\system\ActionConfigEntityInterface;
/** /**
* Builds a form to delete an action. * Builds a form to delete an action.
*/ */
class DeleteForm extends ConfirmFormBase { class ActionDeleteForm extends EntityConfirmFormBase {
/**
* The action to be deleted.
*
* @var \Drupal\system\ActionConfigEntityInterface
*/
protected $action;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getQuestion() { public function getQuestion() {
return t('Are you sure you want to delete the action %action?', array('%action' => $this->action->label())); return t('Are you sure you want to delete the action %action?', array('%action' => $this->entity->label()));
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getConfirmText() { public function getConfirmText() {
return t('Delete'); return t('Delete');
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getCancelPath() { public function getCancelPath() {
return 'admin/config/system/actions'; return 'admin/config/system/actions';
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormID() { public function submit(array $form, array &$form_state) {
return 'action_admin_delete_form'; $this->entity->delete();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, ActionConfigEntityInterface $action = NULL) {
$this->action = $action;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$this->action->delete();
watchdog('user', 'Deleted action %aid (%action)', array('%aid' => $this->action->id(), '%action' => $this->action->label())); watchdog('user', 'Deleted action %aid (%action)', array('%aid' => $this->entity->id(), '%action' => $this->entity->label()));
drupal_set_message(t('Action %action was deleted', array('%action' => $this->action->label()))); drupal_set_message(t('Action %action was deleted', array('%action' => $this->entity->label())));
$form_state['redirect'] = 'admin/config/system/actions'; $form_state['redirect'] = 'admin/config/system/actions';
} }
......
...@@ -370,27 +370,6 @@ function aggregator_save_category($edit) { ...@@ -370,27 +370,6 @@ function aggregator_save_category($edit) {
} }
} }
/**
* Removes all items from a feed.
*
* @param \Drupal\aggregator\Plugin\Core\Entity\Feed $feed
* An object describing the feed to be cleared.
*/
function aggregator_remove(Feed $feed) {
// Call \Drupal\aggregator\Plugin\ProcessorInterface::remove() on all
// processors.
$manager = Drupal::service('plugin.manager.aggregator.processor');
foreach ($manager->getDefinitions() as $id => $definition) {
$manager->createInstance($id)->remove($feed);
}
// Reset feed.
$feed->checked->value = 0;
$feed->hash->value = '';
$feed->etag->value = '';
$feed->modified->value = 0;
$feed->save();
}
/** /**
* Checks a news feed for new items. * Checks a news feed for new items.
* *
......
...@@ -15,14 +15,14 @@ aggregator_admin_settings: ...@@ -15,14 +15,14 @@ aggregator_admin_settings:
aggregator_feed_items_delete: aggregator_feed_items_delete:
pattern: '/admin/config/services/aggregator/remove/{aggregator_feed}' pattern: '/admin/config/services/aggregator/remove/{aggregator_feed}'
defaults: defaults:
_form: '\Drupal\aggregator\Form\FeedItemsDelete' _entity_form: 'aggregator_feed.remove_items'
requirements: requirements:
_permission: 'administer news feeds' _permission: 'administer news feeds'
aggregator_feed_delete: aggregator_feed_delete:
pattern: '/admin/config/services/aggregator/delete/feed/{aggregator_feed}' pattern: '/admin/config/services/aggregator/delete/feed/{aggregator_feed}'
defaults: defaults:
_form: '\Drupal\aggregator\Form\FeedDelete'