processwire / processwire-requests

ProcessWire feature requests.
39 stars 0 forks source link

Add methods to set multilanguage field's values and ML pages' state more easily #475

Open ivangretsky opened 1 year ago

ivangretsky commented 1 year ago

Short description of the enhancement

  1. It would be convenient to have a method to set ML field's values with an associative array with languages names/ids rather than use setLanguageValue() for each language:
    // Second parameter would tell the function whether it should 
    // unset language values not included in the array or not.
    $page->title->setLanguageValues(
        [
            'default' => 'Default title',
            'fr' => 'Com si com sa',
            'ru' => 'Привет, мир',
        ],
        true
    );
  2. It would also be convenient to have methods to activate a page for a number of languages with an associative array with languages names/ids as keys rather than use something like set("status1021", 1) for each language.
    // Second parameter would tell the function whether it should 
    // deactivate a page for languages not included in the array or not.
    $page->setActiveLanguages(
        [
            'default' => true, // for now this is mandatory and could be excluded, but long waited to be possible to set to false
            'fr' => false,
            'ru' => true,
        ],
        true
    );

Why would the enhancement be useful to users?

This would drastically ease the API manipulation of ML pages and fields. And ML is one of the killer features of PW.

ryancramerdesign commented 7 months ago

@ivangretsky Thanks, I have added this, along with a bit more. The setLanguageValues() method was added basically exactly as you mentioned it, except that I didn't want to have a boolean argument that overwrites stuff that isn't specified in the given array. (Though I did add one in the LanguagePageFieldValues class, just in case you really need it). I thought it should only overwrite what is given.

Rather than a setActiveLanguages() method, I added a setLanguageStatus() method, which does the same thing. A setLanguageName() method was also added to accompany it. I thought it made sense with both name and status using the status1234 and name1234 style properties (with 1234 being language ID). These methods can be used to set single values like $page->setLanguageStatus('es', true); or multiple $page->setLanguageStatus([ 'es' => true, 'de' => false ]); Corresponding get() methods were added for all of above as well.