Jump to content

Weekly update – 11 October 2024


ryan
 Share

Recommended Posts

This week there’s new $pages->saveFields() and $page->saveFields() methods on the core dev branch. You might already be familiar with the $pages->saveField($page, $field); method which lets you save one field from a page, or $page->save($field); which does the same. This is useful when you only need to save one field from a page, rather than the entire page. Now we have a plural version of that method, which lets you specify multiple fields on a page to save:

$pages->saveFields($page, [ 'title', 'body', 'summary' ]); 

Below is the same thing, but on a $page object, so you don't need to specify a $page argument:

$page->saveFields([ 'title', 'body', 'summary' ]); 

You can also use a string if you prefer:

$page->saveFields('title,body,summary'); 

In my case, I needed this method for a project I'm working on, and I also needed it to save without updating the 'modified' time or user, which you can achieve by specifying the 'quiet' argument. Though note, the 'quiet' argument is available for all the page saving methods and has been around a long time. But I'm not sure how widely used it is, so I'll mention it. 

$page->saveFields('title,body,summary', [ 'quiet' => true ]); 

This week the API methods for Select Options fields have also been updated to add more convenience for getting, adding and removing options to an existing selection. Let's say we have an Options field of checkboxes named "colors". Each selectable option has an ID, optional value, and title. Now you can get, add, or remove by any of those properties. Previously you had to work directly with SelectableOption instances, which wasn't as convenient. 

// add by title of option 'Blue'
$page->colors->addByTitle('Blue'); 

// remove the option with value 'orange' from colors field
$page->colors->removeByValue('orange'); 

// get SelectableOption instance of option with title 'Red'
$red = $page->colors->getByTitle('Red');
echo "ID, value, title: $red->id, $red->value, $red->title";

// check if colors has an option with value 'purple'
if($page->colors->hasValue('purple')) {
  // it has purple
}

The methods added to SelectableOptionArray (not yet reflected in linked docs page) include:

  • getByID($id)
  • getByValue($value)
  • getByTitle($title)
  • addByID($id)
  • addByValue($value)
  • addByTitle($title)
  • removeByID($id)
  • removeByValue($value)
  • removeByTitle($title)

That's all for this week. There likely won't be any core updates next week, as I'll be out of town again. Thanks for reading and I hope that you all have a great weekend and great week ahead. 

  • Like 20
  • Thanks 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...