Score:0

How to modify the "href" attribute of some of the links of an html field type?

pa flag

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?

id flag
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?
Kevin avatar
in flag
This seems expensive. Is it really the applications burden to worry about 404s vs use something like LinkChecker? https://www.drupal.org/project/linkchecker
LeJuanChis avatar
pa flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.