class FieldDeleteForm

Provides a form for removing a field instance from a bundle.

Hierarchy

Expanded class hierarchy of FieldDeleteForm

File

drupal/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldDeleteForm.php, line 19
Contains \Drupal\field_ui\Form\FieldDeleteForm.

Namespace

Drupal\field_ui\Form
View source
class FieldDeleteForm extends ConfirmFormBase implements ControllerInterface {

  /**
   * The field instance being deleted.
   *
   * @var \Drupal\field\Plugin\Core\Entity\FieldInstance
   */
  protected $instance;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManager
   */
  protected $entityManager;

  /**
   * Constructs a new FieldDeleteForm object.
   *
   * @param \Drupal\Core\Entity\EntityManager $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManager $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.entity'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'field_ui_field_delete_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getQuestion() {
    return t('Are you sure you want to delete the field %field?', array(
      '%field' => $this->instance
        ->label(),
    ));
  }

  /**
   * {@inheritdoc}
   */
  protected function getConfirmText() {
    return t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  protected function getCancelPath() {
    return $this->entityManager
      ->getAdminPath($this->instance->entity_type, $this->instance->bundle) . '/fields';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, FieldInstance $field_instance = NULL) {
    $this->instance = $form_state['instance'] = $field_instance;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
    $field = $this->instance
      ->getField();
    $bundles = entity_get_bundles();
    $bundle_label = $bundles[$this->instance->entity_type][$this->instance->bundle]['label'];
    if ($field && !$field['locked']) {
      $this->instance
        ->delete();
      drupal_set_message(t('The field %field has been deleted from the %type content type.', array(
        '%field' => $this->instance
          ->label(),
        '%type' => $bundle_label,
      )));
    }
    else {
      drupal_set_message(t('There was a problem removing the %field from the %type content type.', array(
        '%field' => $this->instance
          ->label(),
        '%type' => $bundle_label,
      )), 'error');
    }
    $admin_path = $this->entityManager
      ->getAdminPath($this->instance->entity_type, $this->instance->bundle);
    $form_state['redirect'] = "{$admin_path}/fields";

    // Fields are purged on cron. However field module prevents disabling modules
    // when field types they provided are used in a field until it is fully
    // purged. In the case that a field has minimal or no content, a single call
    // to field_purge_batch() will remove it from the system. Call this with a
    // low batch limit to avoid administrators having to wait for cron runs when
    // removing instances that meet this criteria.
    field_purge_batch(10);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText protected function Returns a caption for the link which cancels the action. 2
ConfirmFormBase::getDescription protected function Returns additional text to display as a description. 10
ConfirmFormBase::getFormName protected function Returns the internal name used to refer to the confirmation item.
ConfirmFormBase::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface::validateForm 1
FieldDeleteForm::$entityManager protected property The entity manager.
FieldDeleteForm::$instance protected property The field instance being deleted.
FieldDeleteForm::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides ConfirmFormBase::buildForm
FieldDeleteForm::create public static function Instantiates a new instance of this controller. Overrides ControllerInterface::create
FieldDeleteForm::getCancelPath protected function Returns the page to go to if the user cancels the action. Overrides ConfirmFormBase::getCancelPath
FieldDeleteForm::getConfirmText protected function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
FieldDeleteForm::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
FieldDeleteForm::getQuestion protected function Returns the question to ask the user. Overrides ConfirmFormBase::getQuestion
FieldDeleteForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
FieldDeleteForm::__construct public function Constructs a new FieldDeleteForm object.