Score:0

How to add/insert a value to the end of an array programmatically (in the .theme file) Add a $row

cn flag

I have some code in the .theme file that renders an array list. I wish to add one additional entry at the end.

I found I could do that with a quick and dirty PHP echo statement, and it works just fine, but I would love to know the right way to do this, how to actually insert an entry at the end, one additional $row after all the others. Here's my code so far (that works, but seems inelegant).

function csurg_barrio_preprocess_views_view_summary_unformatted(&$variables) {
    $view = variables['view'];
    $view_display_id = $view->getDisplay()->display['id'];

    if ($view->id() === 'content_display' && $view_display_id === 'attachment_1') { // create a list of filters from a to z
        $rows = $variables['rows'];
        foreach ($rows as $row) {
            // kint($row);
            $url = $row->url;
            $link = $row->link;
            $wanted_url = '/conditions-and-treatments/' . $link;
            if ($url !== $wanted_url) {
                $row->url = $wanted_url;
            }
        }
        // append a value to the end of the list
        // $url = "/conditions-and-treatments/all";
        // $link = "Full List";
        // $row->url = $url;
        // the below works but is not particularly elegant
        echo '<span class="views-filter-all-option views-summary views-summary-unformatted">&nbsp;| <a href="/conditions-and-treatments/all">Full List</a></span>';
    }

}

What's a better way to achieve this?
Screenshot from VSCode

leymannx avatar
ne flag
Don't `echo`. Instead add the markup or a render element to the `$variables` (e.g. `$variables['foobar']` and then print it in your template (e.g. `{{ foobar }}`).
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.