Background information: I'm trying to manipulate the available options of a list type text field, depending on the host bundle:
function wtbase_options_list_alter(array &$options, array $context) {
$fieldDefinition = $context['fieldDefinition'];
if ($fieldDefinition->getName() == 'field_aos_cell' && $fieldDefinition->bundle() != 'link') {
foreach ($options as $key => $value) {
if ($key == 'link-horizontally-opposing') {
unset($options[$key]);
}
}
}
}
The $fieldDefinition->bundle
part is really confusing. There seems to be a variable $fieldDefinition->bundle
(containing the expected bundle name link
) and a function $fieldDefinition->bundle()
(containing field_config
). But I can't access the variable bundle
, because this crashed with Cannot access protected property Drupal\field\Entity\FieldConfig::$bundle
.
After some digging I found FieldConfigBase::getTargetBundle(), which did return the expected result and did not crash (yet).
So this leads to two questions:
What is the difference between $bundle
, bundle()
and getTargetBundle()
and which one I'm supposed to use? I'd prefer using variables, because they are visible in Xdebug, while functions are not.
And in a more general point of view, are there some guidelines when to use functions and when to use variables? In the same example as above Xdebug printed the same weird results for e.g. entity_type
vs entityTypeId
vs getEntityType()
vs getEntityTypeId()
.
--
Update, clarification for question 1:
I know the technical difference between a member property and a member function. But I'm deeply confused that a property $bundle
and a function bundle()
of the exact same name are returning values from a completely different entities/context. In my example $fieldDefinition->bundle
returns a Paragraph bundle name link
, while $fieldDefinition->bundle()
seems to return the bundle name from FieldConfig. I know this is technically possible... but why would anyone write code like this? I've not yet experienced this behavior anywhere else in Drupal core, usually $entity->bundle
and $entity->bundle()
return the same result.