4

Can you wonderful people help me out please? I am altering the field config form (content types > plan > manage fields > edit field) and cannot seem to get the proper bundle out. Every time I call the bundle() function it returns the string "field_config". However the bundle is supposed to be "plan". Perhaps it's different because I am on the field config form, and not a node form?

Below is my code, the things I've tried and the field object showing the proper bundle. Does anyone know of another solution for me to get "plan" out?

<?php

use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\ContentEntityForm;

/**
 * Implements hook_form_alter().
 */
function rent_plan_custom_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// print $form_id;

  switch ($form_id) {

    case 'field_config_edit_form':

        print $form['#title'];

        $field = $form_state->getFormObject()->getEntity();
        $field_name = $field->getName();  //returns correct field_plan_purpose

        //first try
        print $field->bundle(); //<---THIS RETURNS "field_config"

        //second try
        $bundle = FALSE;
        if ($form_object = $form_state->getFormObject()) {
            if ($form_object instanceof Drupal\Core\Entity\ContentEntityForm) {
                $bundle = $form_object->getEntity()->bundle();
            } elseif ($form_object instanceof Drupal\field_ui\Form\FieldConfigEditForm) {
                $bundle = $form_object->getEntity()->bundle(); 
            }
            print $bundle; //<---THIS RETURNS "field_config"
        }

        //Field Object Results Below
        print '<pre>';
        print_r($field);
        print '</pre>';


    break;
  }
}

Results from print_r($field)

Drupal\field\Entity\FieldConfig Object
(
    [deleted:protected] => 
    [fieldStorage:protected] => Drupal\field\Entity\FieldStorageConfig Object
        (
            [id:protected] => node.field_plan_purpose
            [field_name:protected] => field_plan_purpose
            [entity_type:protected] => node
            [type:protected] => text_long
            [module:protected] => text
            [settings:protected] => Array

            ... lots of more stuff
        )

    [id:protected] => node.plan.field_plan_purpose
    [field_name:protected] => field_plan_purpose
    [field_type:protected] => text_long
    [entity_type:protected] => node
    [bundle:protected] => plan
    [label:protected] => Purpose
    [description:protected] => 
    [settings:protected] => Array
        (
        )

    ...lots more stuff
)
5

You're looking for FieldDefinitionInterface::getTargetBundle

Gets the bundle the field is attached to.

This method should not be confused with EntityInterface::bundle() (configurable fields are config entities, and thus implement both interfaces):

  • FieldDefinitionInterface::getTargetBundle() answers "as a field, which bundle are you attached to?".
  • EntityInterface::bundle() answers "as a (config) entity, what is your own bundle?" (not relevant in our case, the config entity types used to store the definitions of configurable fields do not have bundles).

e.g.

$target_bundle = $field->getTargetBundle();
  • Thank you! This was perfect for my needs and I see now how these methods are defined in FieldConfigBase – Matt Campbell Nov 28 '16 at 19:30
3

FieldConfigBase has a method on it(*) called getTargetBundle(). That will retrieve the field name. To be safe, you should also probably use getTargetEntityTypeId() to make sure you are dealing with a node:

$entity_type = $field->getTargetEntityTypeId();
$bundle = $field->getTargetBundle();
if ($entity_type === 'node' && $bundle === 'plan') {
  do_something_awesome();
}

Using a class-aware editor, like PHPStorm, will make things like this a lot easier because you add type-hints, and then it will autocomplete for you. In most OO code and some procedural code, a good IDE will automagiclly figure out your types, so you don't need to add the hinting.

(*) You should really be looking at the interface(s) that these define. The one of interest in this case is FieldDefinitionInterface.

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.