FieldDeleteForm.php 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php

/**
 * @file
 * Contains \Drupal\field_ui\Form\FieldDeleteForm.
 */

namespace Drupal\field_ui\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\field\Plugin\Core\Entity\FieldInstance;

/**
 * Provides a form for removing a field instance from a bundle.
 */
class FieldDeleteForm extends ConfirmFormBase {

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

  /**
   * {@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 field_ui_bundle_admin_path($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 = field_ui_bundle_admin_path($this->instance->entity_type, $this->instance->bundle);
    $form_state['redirect'] = field_ui_get_destinations(array($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);
  }

}