0

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?

3
  • 1
    Without even looking at the details, you should be using an entity field query for nodes and editing and saving nodes with the entity API. Is revisioning in effect?
    – mona lisa
    Feb 28, 2023 at 23:04
  • 1
    This seems expensive. Is it really the applications burden to worry about 404s vs use something like LinkChecker? drupal.org/project/linkchecker
    – Kevin
    Mar 1, 2023 at 0:00
  • I tried to do it with the entity api and using the class "DOMDcument" but when I try to make the change and save the paragraph, the whole field appears as if it were an html, it does not save the updated text but it saves the whole html. And I also tried with LinkChecker but I need something a little more customized to make the poll.
    – LeJuanChis
    Mar 1, 2023 at 15:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.