Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupalrecipe_searchForm;
  4.  
  5. use DrupalCoreFormFormStateInterface;
  6. use DrupalCoreFormConfirmFormBase;
  7. use DrupalCoreUrl;
  8.  
  9. /**
  10. * Class DeleteIngredient
  11. *
  12. * @package Drupalrecipe_searchForm
  13. */
  14. class DeleteIngredient extends ConfirmFormBase{
  15.  
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getFormId() {
  20. return 'delete_ingredient';
  21. }
  22.  
  23. public $cid;
  24.  
  25. public function getQuestion() {
  26. return t('Do you want to delete %cid?', array('%cid' => $this->cid));
  27. }
  28.  
  29. public function getCancelUrl() {
  30. return new Url('recipe_search.recipes');
  31. }
  32.  
  33. public function getDescription() {
  34. return t('Only do this if you are sure!');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getConfirmText() {
  40. return t('Delete it!');
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getCancelText() {
  46. return t('Cancel');
  47. }
  48.  
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {
  53. $this->pid = $cid;
  54. return parent::buildForm($form, $form_state);
  55. }
  56.  
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function submitForm(array &$form, FormStateInterface $form_state) {
  61.  
  62. $database = Drupal::database();
  63. $database->delete('ingredient_list')
  64. ->condition('pid', $this->pid)
  65. ->execute();
  66.  
  67. $form_state->setRedirect('recipe_search.recipes');
  68.  
  69. }
  70.  
  71. <?php
  72.  
  73. namespace Drupalrecipe_searchForm;
  74.  
  75. use DrupalCoreFormFormBase;
  76. use DrupalCoreFormFormStateInterface;
  77.  
  78. /**
  79. * Class DeleteIngredient
  80. *
  81. * @package Drupalrecipe_searchForm
  82. */
  83. class DeleteIngredient extends FormBase{
  84.  
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getFormId() {
  89. return 'delete_ingredient';
  90. }
  91.  
  92. public $cid;
  93.  
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {
  98. $this->pid = $cid;
  99. return parent::buildForm($form, $form_state);
  100. }
  101.  
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function submitForm(array &$form, FormStateInterface $form_state) {
  106.  
  107. $database = Drupal::database();
  108. $database->delete('ingredient_list')
  109. ->condition('pid', $this->pid)
  110. ->execute();
  111.  
  112. $form_state->setRedirect('recipe_search.recipes');
  113.  
  114. }
  115. }
  116.  
  117. //FETCH THE INGREDIENTS TO MAKE AN API CALL
  118. $result = $connection->select('ingredient_list', 'il');
  119. $result->fields('il', array('ingredient', 'pid'));
  120. $result->condition('uid', $uid);
  121. $data = $result->execute();
  122. $results = $data->fetchAll(PDO::FETCH_OBJ);
  123.  
  124. foreach ($results as $field) {
  125. $delete = Url::fromUserInput('/delete/'.$field->pid);
  126.  
  127. array_push($ingredients, ["ingredient" => $field->ingredient, "delete" => $delete]);
  128. array_push($ingredientsArray, $field->ingredient);
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement