0

I have a paragraph field in a node with unlimited add paragraph. I am removing these paragraph programatically. Its removing but when I am adding a new paragraph its adding blank paragraph along with.

I am using this code to remove paragraph

use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\Entity\Paragraph;

$ids = \Drupal::entityQuery('paragraph')->execute();
foreach ($ids as $id) {
  $paragraph = Paragraph::load($id);
  if ($paragraph instanceof ParagraphInterface) {
    $paragraph->delete();
  }
}
  • What are you doing there exactly and why? Where are you executing these deletions? Why don't you use the UI? – leymannx Feb 4 at 7:55
  • Its a part of logic. – DrupalUser Feb 4 at 8:34
4

You have 3 tables doing the job: a table which stores node data, a table which stores paragraphs data, and a table which stores relation about node and paragraphs. Now you emptied the table which stores the paragraphs data, but the node-paragraph relation table is still there. So if you have added 4 paragraphs to a node and deleted that 4 paragraphs, still drupal assumes there should be 4 paragraphs attached to that node.


so if you want to delete them entirely you have to fetch node data which has "paragraph" field value, delete field value from each node and save it.

so here are steps : you can run a database query on field table, and get all node-paragraph relations. now you have a list of all nodes that have a paragraphs, so having node ids and paragraph ids you can first remove paragraphs from node using a code like this:

$node = Node::load($nid);
$node->FIELD_NAME->setValue(array());
$node->save();

and then delete correspond paragraphs using a code like the one you mentioned. of course you don't need another query as you have paragraph ids.


if you don't mind removing all paragraph values from database and only don't like them to be shown in your node, then you can write an entityquery like this :

$query = \Drupal::entityQuery('node');
$query->exists('field_to_check');//Check A field value is NOT NULL
$ids = $query->execute();

so it gives you all nodes which has value for that field, then you only can run :

foreach($ids as $nid) {
  $node = Node::load($nid);
  $node->FIELD_NAME->setValue(array());
  $node->save();
}
|improve this answer|||||
  • is it possible to delete this paragraph? – DrupalUser Feb 4 at 9:11
  • Answer was updated ;) – Alireza Tabatabaeian Feb 4 at 9:32
  • $node->FIELD_NAME->setValue(array()); what value need to set here? – DrupalUser Feb 4 at 9:35
  • because you want to empty all the relations an empty array is passed as value, so it cause drupal to remove all relations(consider this field as empty) – Alireza Tabatabaeian Feb 4 at 9:39
  • I Also added a new answer where you save your paragraphs in DB but remove them from your node – Alireza Tabatabaeian Feb 4 at 9:45

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.