0

I am trying to add a form field in a existing config entity form. The value of the field is stored in the data blob of the correct config entity but I am not able to get it out of it.

/**
* Implements hook_form_FORM_ID_alter().
*/
function my_module_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
 $someEntity = \Drupal::entityTypeManager()->getStorage('some entity');
 $form['my-field'] = [
  '#type' => 'number',
  '#title' => 'some title',
  '#weight' => 1,
  '#min' => '0',
  '#max' => '50',
  '#default-value' => $someEntity->getValue('my-field'), // Here I would like to get the Value that is stored in the db
 ];
 $form['#validate'][] = 'my_module_field_config_edit_form_validate';     
}  

@Berdir

I tried your answer but the value still won't be set as default_value

    /**
     * Implements hook_form_FORM_ID_alter().
     */
function comment_limit_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
  $comment = $form_state->getFormObject()->getEntity();
  $form['edit-limit-per-user'] = [
    '#type' => 'number',
    '#title' => 'Comment limit per user',
    '#weight' => 1,
    '#min' => '0',
    '#max' => '50',
    '#default_value' => $comment->getThirdPartySetting('comment_limit', 'edit-limit-per-user', FALSE),
  ];
  $form['#validate'][] = 'comment_limit_field_config_edit_form_validate';
  $form['#entity_builders'][] = 'comment_limit_form_field_edit_form_add_form_builder';
}

/**
 *
 */
function comment_limit_form_field_edit_form_add_form_builder($entity_type, FieldConfig $comment , &$form, FormStateInterface $form_state) {
  $comment->setThirdPartySetting('comment_limit', 'edit-limit-per-user', $form_state->getValue('edit-limit-per-user'));
}  


comment.type.comment.third_party.comment_limit:
  type: mapping
  label: 'Comment limit per user'
  mapping:
    edit_limit_per_user:
      type: number
      label: 'Comment limit per user'

Did I miss something?

This works for me:)

  • 1
    ``` $someEntity = \Drupal::entityTypeManager()->getStorage('some entity'); ``` This for itself can't work. You need to at least call something like ->load() otherwise you don't have the entity available. getStorage() just returns a class which deals with generic storage of an entity type – Daniel Wehner Aug 22 '16 at 14:20
2

The entity is already available with $form_state->getFormObject()->getEntity(). That basically answers your question.

Note that stuffing arbitrary keys into a config entity is not supported, you should use the third party settings API instead. See http://larowlan.github.io/meet-third-party-settings-interface/#/12 for a simple example (but make sure you also check the slides before about config schema). The initial part is not necessary anymore as all config entities support third party settings out of the box now.

  • Any idea why my IDE doesn't recognize the "getEntity()" method/function? – Insasse Aug 23 '16 at 6:50
  • 1
    Yes, because only entity forms have that method. you can do $form_object = ... and then document that with an @var as EntityForm or do an instanceof EntityForm, but if you implement a specific form id, you can be sure that you have such an object. Would be different if you implement a generic hook_form_alter() and want to run on all entity forms, then you'd need the instanceof. – Berdir Aug 23 '16 at 6:58
  • 1
    And yes, you missed something. It's #default_value, not #default-value. #properties never use - – Berdir Aug 23 '16 at 6:59

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.