Problem

  • Consuming code of more advanced config entities, typically with a $settings property, need a way to access sub-keys within $settings, without having to copy the entire $settings into a separate local variable first; i.e.:
    - $settings = $block->get('settings');
    - $id = isset($settings['id']) ? $settings['id'] : NULL;
    + $id = $block->get('settings.id');
    

Proposed solution

  1. Import NestedArray in ConfigEntityBase.
  2. Change ConfigEntityBase::get() (+ set()) to support dot-delimited property names, retaining all other/original Entity::get() logic, and just performing the additional NestedArray::get() logic in the end:
      $value = ...;
      return NestedArray::get($value, explode('.', $name));
    

Comments

tim.plunkett’s picture

I think #1846454: Add Entity query to Config entities has support for matching by nested property names:

As a bonus, we not just match display.default.display_options.path but display.*.display_options.path as well

sun’s picture

Yep, that one is limited to entity queries though.

This issue here talks about $entity->get('foo.bar.baz')

tim.plunkett’s picture

Yes, sorry, I wasn't suggesting that the other issue replaces this one functionally, just that we can look to it for implementation.

xjm’s picture

Issue summary: View changes
Issue tags: +Naming things is hard

Is this still relevant?

Arla’s picture

In the set() case, this

if (isset($entity->settings)) {
  $entity->settings['property'] = 'value';
}
else {
  $entity->settings = array('property' => 'value');
}

would turn into pretty

$entity->set('settings.property', 'value');
tim.plunkett’s picture

Version: 8.0.x-dev » 8.1.x-dev

I think we can do this without a noticeable API break.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.