Jump to content

add to array of selected options via API


froot
 Share

Recommended Posts

I have an array of bilingual pages. These pages have a body. If the body is filled, it's in the array. So the array is different in english than in german. Criteria is the body. I want to change that though and use a SelectOptions field as criteria instead. That field would be a multiple value checkbox SelectOptions field with values as follows.

1=English
2=Deutsch

Now I want to check one or two of these options for each page via API.

Something like:

foreach ($suckers as $s) { // :D

  if (user()->language->title == 'Deutsch') {

      $s->of(false);

      if ($s->cast_language == "") {
          $s->cast_language = "2";
      } elseif ($s->cast_language == "1") {
          $s->cast_language = "1|2";
      }

      $s->cast_language->items->add(1);

      $s->save();
      $s->of(true);
  }

  if (user()->language->title == 'English') {
      $s->of(false);

      if ($s->cast_language == "") {
          $s->cast_language = "1";
      } elseif ($s->cast_language == "2") {
          $s->cast_language = "1|2";
      }

      $s->cast_language->items->add(1);

      $s->save();
      $s->of(true);
  } 

}

But I think that's nonsense, still cannot tell from the documentation how to approach this.

The issue is, the array depends on the user's current language, so in order to use this function to set the values right I need to run it once in German and once in English. But at the second run it will overwrite the values of the first run. Don't know how to add to the array of selected options.

Thanks for help!

Link to comment
Share on other sites

I read this quickly and I don't understand it fully. One thing to note: this

<?php
if (user()->language->title == 'English') {
//
}

and this

<?php

if (user()->language->title == 'Deutsch') {
// 
}

Will never be true at the same time since $user can only have one language at a time. Why not use if / else if? Even better, since your site is bilingual, the language can just be one or the other, i.e.

<?php

if (user()->language->title == 'Deutsch') {
// German
}
else {
// must be English here
}

Other than that, I have no idea what you are trying to accomplish ?, sorry.

  • Like 1
Link to comment
Share on other sites

what I want to accomplish is to actually allow the pages to be english AND/OR german, so they can in fact be english AND german. I want to give the content manager full control over which page is shown in which language. So the problem remains that when I run my code, the value of the selectoptions field gets overwritten and I just don't know how to simply add to the array instead. I'm sure it must be possible somehow.

Link to comment
Share on other sites

1 hour ago, fruid said:

So the problem remains that when I run my code, the value of the selectoptions field gets overwritten and I just don't know how to simply add to the array instead.

OK. What is cast_language? The select options field. What is array you need to add to?

Link to comment
Share on other sites

cast_language is the select options field's name. It has options (multiple checkboxes) as follows:

1=English
2=Deutsch

That matches the title of the available languages.

I edited my original post and added the foreach loop to make things clearer.

Link to comment
Share on other sites

OK I'm  a step further. I thought the following would work but it does not:

$suckers = $pages->find("template=cast, include=all, sort=id");
$language = user()->language->title;
$language = strval($language);

foreach ($suckers as $s) {
	if ($s->body != '') {
		$s->of(false);
		$s->cast_language = $s->cast_language->title.'|'.$language;				
		$s->save('cast_language');
		$s->cast_language->save();
		$s->of(true);
	} else {
		continue;
	}
}

Don't know where the issue is, it seems to not really save it properly. I never know which one to use:

$s->save();
$s->save($field);
$s->$field->save();

Thanks for help.

Link to comment
Share on other sites

on a side-note, I find the PW API quite inconsistent, anything I try to solve with the API to avoid doing changes by hand involves a lot of research and forum thread writing and reading and trial and error, because every field seems to work a bit differently, and so it's really time consuming and defeats the purpose of avoiding to do it manually.

Link to comment
Share on other sites

What do you expect $s->cast_language->title to be here?

$s->of(false);
$s->cast_language = $s->cast_language->title.'|'.$language;				
$s->save('cast_language');

As I understand it, cast_language allows multiple selection, so it’s nonsensical to access an option’s title without determining a singular option first. Multiple selection options fields have WireArray values, so I feel like this might work:

$s->of(false);
$s->cast_language = $s->cast_language->implode('|', 'title') . '|' .$language;				
$s->save('cast_language');

I agree that’s a bit ugly, but there is probably a better way. I’m guessing $s->cast_language->add($language) won’t work because it’s not a WireArray of strings. Probably only works if you pass a SelectableOption.

 

2 hours ago, fruid said:

I never know which one to use:

$s->save();
$s->save($field);
$s->$field->save();

Honestly, just always use $page->save() (in this case $s->save()), unless you have a specific reason not to.

  • Like 1
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...