In a Drupal 8 instance, getThirdPartySetting() function to save node type configurations using the NodeTypeInterface object like below

  $type = $form_state->getFormObject()->getEntity();
  $fields = $type->getThirdPartySetting('field_config', 'type_fields', array(''));

Now I need to use the configurations in a hook for validation, to be particular in hook_entity_bundle_field_info_alter()

function hook_entity_bundle_field_info_alter(&$fields
  , \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle)

It contains objects of EntityTypeInterface but not that of NodeTypeInterface. Now how can I access the configuration of the particular node type in the hook?

When I tried something like this in hook_entity_bundle_field_info_alter:

  $node_type = \Drupal\node\Entity\NodeType::load($bundle);
  $selected_fields = $node_type->getThirdPartySetting('field_config',
    'field_scope', '');

It throws an error as : Call to a member function getThirdPartySetting() on a non-object


$bundle is the node type (ID), so you can load the node type based on that.

$node_type = \Drupal\node\Entity\NodeType::load($bundle);
  • thanks for your response. But how can I get an object of NodeTypeInterface class. It contains the function getThirdPartySetting. Jun 25 '15 at 7:03
  • Exactly like I said? $node_type is the loaded node type entity. You have getThirdPartySettings() on there.
    – Berdir
    Jun 25 '15 at 7:06
  • I updated my question please check it. It throws me an error if used getThirdPartySettings(). I can't even print to see how the object looks inside the hook. :( Jun 25 '15 at 7:17
  • I guess that means that you are missing a check if the entity type actually is node. It could also be something else like user or so.
    – Berdir
    Jun 25 '15 at 7:19
  • That's a good catch berdir. I also confused how to pass the $options argument in addConstraint() in hook_entity_bundle_field_info_alter. I will start a new post on it. Jun 25 '15 at 7:25

There is scenario in Drupal 8, retrieve the third party settings of "field_image" inside the preprocess function. below code used to getThirdPartySettings values.

$node_type = \Drupal::entityManager()->getFieldDefinitions('node', 'bundlename');
$settings = $node_type['field_image']->getThirdPartySetting('mymodule', 'field_scope');

It is working fine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.