class TableDragExampleResetForm

Same name in other branches
  1. 3.x modules/tabledrag_example/src/Form/TableDragExampleResetForm.php \Drupal\tabledrag_example\Form\TableDragExampleResetForm
  2. 4.0.x modules/tabledrag_example/src/Form/TableDragExampleResetForm.php \Drupal\tabledrag_example\Form\TableDragExampleResetForm

Table drag example reset form.

@package Drupal\tabledrag_example\Form

Hierarchy

Expanded class hierarchy of TableDragExampleResetForm

1 string reference to 'TableDragExampleResetForm'
tabledrag_example.routing.yml in tabledrag_example/tabledrag_example.routing.yml
tabledrag_example/tabledrag_example.routing.yml

File

tabledrag_example/src/Form/TableDragExampleResetForm.php, line 17

Namespace

Drupal\tabledrag_example\Form
View source
class TableDragExampleResetForm extends ConfirmFormBase {
    
    /**
     * The database connection.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $database;
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('database'));
    }
    
    /**
     * Construct a form.
     *
     * @param \Drupal\Core\Database\Connection $database
     *   The database connection.
     */
    public function __construct(Connection $database) {
        $this->database = $database;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'tabledrag_example_reset';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getQuestion() {
        return $this->t('Reset demo data for TableDrag Example');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCancelUrl() {
        return new Url('tabledrag_example.description');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDescription() {
        return $this->t('Are you sure you want to reset demo data?');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConfirmText() {
        return $this->t('Yes, Reset It!');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCancelText() {
        return $this->t('Cancel');
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $data = Fixtures::getSampleItems();
        foreach ($data as $id => $item) {
            // Add 1 to each array key to match ID.
            $id++;
            $this->database
                ->update('tabledrag_example')
                ->fields([
                'weight' => 0,
                'pid' => 0,
                'description' => $item['description'],
                'itemgroup' => $item['itemgroup'],
            ])
                ->condition('id', $id, '=')
                ->execute();
        }
        $this->messenger()
            ->addMessage($this->t('Data for TableDrag Example has been reset.'), 'status');
        $form_state->setRedirect('tabledrag_example.description');
    }

}

Members