(DX) Civi::contactSettings - Add a facade for working with the logged-in user's settings
authorTim Otten <totten@civicrm.org>
Thu, 27 Sep 2018 21:05:06 +0000 (17:05 -0400)
committerTim Otten <totten@civicrm.org>
Thu, 27 Sep 2018 21:05:06 +0000 (17:05 -0400)
commit01d70c562c189135c1d0012646ca1dde1112af9d
tree64f014932abd0811b4a4785022875a113e70d95d
parent56afc088f70196cddc7ea55dd05bc48a3cdc2999
(DX) Civi::contactSettings - Add a facade for working with the logged-in user's settings

Before
------

To read/write a setting for the logged-in user, you need a snippet like this:

```php
$cid = CRM_Core_Session::getLoggedInContactID();
$myFilter = Civi::service('settings_manager')
  ->getBagByContact(NULL, $cid)
  ->get('activity_tab_filter');
```

After
-----

To read/write a setting for the logged-in user, you can use a snippet like this:

```php
$myFilter = Civi::contactSettings()->get('activity_tab_filter');
```

Technical Details
-----------------

* A convenience function like this is liable to be used in lazy circumstances where
  the developer is unlikely to check their conditions carefully. Therefore, the
  errors are reported as exceptions so that mistakes are easiliy revealed.
* This is technically a small contract change to `SettingsManager::getBagByContact()`
  because it now interprets `$contactId===NULL` as meaning "the logged-in user".
  However, I don't think NULL was a sensible value before, and this interface
  isn't widely known/used.
Civi.php
Civi/Core/SettingsManager.php
tests/phpunit/Civi/Core/CiviFacadeTest.php [new file with mode: 0644]