0

Drupal 8.7.x

I have a Form button with a confirmation page to delete an ingredient. I'm trying to convert it to a standard Form that just deletes it without confirmation, however I keep getting an error.

Confirmation Form (works just fine) src/Form/DeleteIngredient.php:

 <?php

namespace Drupal\recipe_search\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;

/**
 * Class DeleteIngredient
 *
 * @package Drupal\recipe_search\Form
 */
class DeleteIngredient extends ConfirmFormBase{

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'delete_ingredient';
  }

  public $cid;

  public function getQuestion() {
    return t('Do you want to delete %cid?', array('%cid' => $this->cid));
  }

  public function getCancelUrl() {
    return new Url('recipe_search.recipes');
  }

  public function getDescription() {
    return t('Only do this if you are sure!');
  }
  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return t('Delete it!');
  }
  /**
   * {@inheritdoc}
   */
  public function getCancelText() {
    return t('Cancel');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {
    $this->pid = $cid;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $database = \Drupal::database();
    $database->delete('ingredient_list')
      ->condition('pid', $this->pid)
      ->execute();

    $form_state->setRedirect('recipe_search.recipes');

  }

Tried this for normal form without confirmation src/Form/DeleteIngredient.php:

<?php

namespace Drupal\recipe_search\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class DeleteIngredient
 *
 * @package Drupal\recipe_search\Form
 */
class DeleteIngredient extends FormBase{

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'delete_ingredient';
  }

  public $cid;

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {
    $this->pid = $cid;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $database = \Drupal::database();
    $database->delete('ingredient_list')
      ->condition('pid', $this->pid)
      ->execute();

    $form_state->setRedirect('recipe_search.recipes');

  }
}

This is where I return the form to the twig template src/Block/RecipeSearch.php:

//FETCH THE INGREDIENTS TO MAKE AN API CALL
$result = $connection->select('ingredient_list', 'il');
$result->fields('il', array('ingredient', 'pid'));
$result->condition('uid', $uid);
$data = $result->execute();
$results = $data->fetchAll(\PDO::FETCH_OBJ);

foreach ($results as $field) {
  $delete = Url::fromUserInput('/delete/'.$field->pid);

  array_push($ingredients, ["ingredient" => $field->ingredient, "delete" => $delete]);
  array_push($ingredientsArray, $field->ingredient);
}; 

This is the Error I'm getting:

Error: Cannot call abstract method Drupal\Core\Form\FormInterface::buildForm() in Drupal\recipe_search\Form\DeleteIngredient->buildForm() (line 32 of /var/www/html/web/modules/custom/recipe_search/src/Form/DeleteIngredient.php)

I guess you cannot use buildForm while extending FormBase but I don't know how I should solve this.

Thanks in advance!

1

There is no FormBase::buildForm() method. FormBase implements FormStateInterface, and the method FormStateInterface::buildForm() exists. So when you extend FormBase, you must implement buildForm(), however you do not/cannot call parent::buildForm(). Instead, you must build the form array yourself, and return that.

This differs from ConfirmForm which has a buildForm() method, which is why when you extend ConfirmForm you can call parent::buildForm().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.