Jump to content

issues with setting the name of multilingual pages


fruid
 Share

Recommended Posts

I have a multilingual PW installation with

default (Deutsch)
english (English)

I have imported a lot of pages and I created a Admin Action that lets me upload a CSV file and set the fields on the page which worked fine. 

I'm now trying to set the names of the german and the english page according to the page's title.

so I here's my code:

    protected function executeAction($options) {
            
        $url = config()->paths->assets.'cache/AdminActions/';
            
        if (count($options['csv_upload'])) {
            $n = 0;
            $file = $options['csv_upload']->first();
            $fp = fopen($file->filename, 'r');
            $count = 0;
                                            
            while (($row = fgetcsv($fp, 0, ',', '"')) !== false) {
                $count++;

                if (++$n === 1) {
                    // Store the field names based on the first row of the CSV
                    $fieldNames = $row;
                    continue;
                }

                // Get the page_id from the first column of the current row
                $page_id = $row[0];

                $target_page = pages()->get("id=$page_id");

                if ($target_page->id) {
                    $target_page->of(false);

                    // Set the field values dynamically based on the field names and row values
                    foreach ($fieldNames as $index => $fieldName) {
                        if ($fieldName !== 'page_id') {
                            $fieldValue = $row[$index];
                            $target_page->set($fieldName, $fieldValue);
                            if ($fieldName == 'title_de') {
                                $name = wire('sanitizer')->name($fieldValue, true, 128, '-');
                                $target_page->setName($name, 'default');
                            }
                            if ($fieldName == 'title_en') {
                                $name = wire('sanitizer')->name($fieldValue, true, 128, '-');
                                $target_page->setName($name, 'english');
                            }
                        }
                    }

                    $target_page->save();
                    $target_page->of(true);
                } else {
                    $this->error("$target_page is not a project");
                    continue;
                }

                $target_page->of(true);
            }            

            $this->successMessage = "$count CSV rows were processed.";
                
            fclose($fp);
            $this->wire('files')->unlink($file->filename);
            return true;
        } else {
            $this->failureMessage = 'Please upload a CSV file';
            return false;
        }

I managed to set all other multilingual fields of the pages just fine when having the right language active for my user. 
It seems to be different for the name though, I think I need to use

$target_page->setName($name, 'english');

for it to work.

However, the results are inconsistent, especially when I have multiple pages with the same title. (German page gets the English name, English name doesn't get a -1 (or -2, or -3) added to the end). I cannot quite put my finger on how that works exactly, but it seems to depend on whichever language is active for my user.

First I thought the language status of the page (status1021 for english in my case) cause the problem, it was set to 0, so I changed that to 1 but that didn't help either. 

So now I ended up with inconsistencies, e.g. the same name and url for pages with the same title which results in a 404 for all of them. And now even changing it manually doesn't fix it. 

Thanks for help!


 

Link to comment
Share on other sites

I'm trying to boil it down but it's difficult to say what the issue is.

I have e.g. 4 pages with the same title. In the default language (or maybe in the activated language for this user?) the naming of the page, when derived from the title, iterates i.e. a hyphen and a number is added at the end HOWEVER that doesn't seem to work when the letter format doesn't match

so "Some Page" and "some page" get interpreted as different titles hence get the exact same name, which causes troubles (404)

AND in whichever language that is not default (or not activated for this user?) the names do NOT iterate in the same fashion, i.e. do NOT get a hyphen and a number at the end.

I tried @adrian's module PageRenameOptions but not sure how this works. Does it, on submission, change and save all the pages? Or overrides them? It seems not solve the above mentioned problems anyway…

thanks for help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...