I'm using drupal 9.4.8 and I have a content type called "articles" which has a "body" field (which is a reference to a paragraph) and I'm making an algorithm to check all the articles already loaded and in its body field check when links are there and look if its "href" attribute has a valid code, otherwise replace the content of its "href" attribute with a "#".
So far I did it through a query to the DB like this:
$database = \Drupal::service('database');
$query = $database->select('paragraph__field_summary')
->fields('paragraph__field_summary')
->range(0, 40);
And after that I do the algorithm to replace it in the database:
$query = $database->select('paragraph__field_summary')
->fields('paragraph__field_summary')
->condition('entity_id', 10123);
$result = $query->execute();
$dom = new DOMDocument();
foreach ($result as $record) {
$str = $record->field_summary_value;
$dom->loadHTML($str);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$search_url = $link;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $search_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
/* if (!curl_errno($curl)) { */
switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
case 404:
dump($search_url);
exit;
$str = str_replace($search_url, "#", $str);
$str = str_replace('target="_blank"', " ", $str);
$status_update = update_node($record, $str);
break;
/* } */
}
curl_close($curl);
}
}
code of the function "update_node":
try {
$database = \Drupal::service('database');
$num_updated = $database->update('paragraph__field_summary')
->fields([
'field_summary_value' => $data_replace,
])
->condition('entity_id', $data_principal->entity_id, '=')
->execute();
$num_updated2 = $database->update('paragraph_revision__field_summary')
->fields([
'field_summary_value' => $data_replace,
])
->condition('entity_id', $data_principal->entity_id, '=')
->execute();
return $num_updated;
} catch (\Exception $e) {
return $e;
}
When it is executed it makes me the change in the database in phpmyadmin, but at the time of opening the node the change is not noticed. I was thinking that maybe it is a cache issue but I tried several ways to clear cache and none of them worked, maybe someone has some idea that can help me?