0

I created a custom content entity with Drupal Console.
Three of its fields are defined this way:

// ----------------------------------------------------------------------------
    $weight++;
    $fields['picture'] = BaseFieldDefinition::create('image')
      ->setLabel(t('Picture'))
      ->setSettings([
        'file_extensions' => 'gif jpeg jpg png',
        'file_directory' => 'sel',
        'alt_field_required' => 0,
      ])
      ->setCardinality(3)
      ->setDisplayOptions('view', [
        'type' => 'default',
        'weight' => $weight,
      ])
      ->setDisplayOptions('form', [
        'type' => 'image_image',
        'weight' => $weight,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
// ----------------------------------------------------------------------------
    $weight++;
    $fields['file'] = BaseFieldDefinition::create('file')
      ->setLabel(t('File'))
      ->setSettings([
        'file_extensions' => 'pdf',
        'file_directory' => 'sel',
        'description_field' => TRUE,
      ])
      ->setDisplayOptions('view', [
        'type' => 'file',
        'weight' => $weight,
      ])
      ->setDisplayOptions('form', [
        'type' => 'file',
        'weight' => $weight,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
// ----------------------------------------------------------------------------
    $weight++;
    $fields['link'] = BaseFieldDefinition::create('link')
      ->setLabel(t('Link'))
      ->setDisplayOptions('view', [
        'type' => 'link',
        'weight' => $weight,
      ])
      ->setDisplayOptions('form', [
        'type' => 'link_default',
        'weight' => $weight,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
// ----------------------------------------------------------------------------

And the corresponding form looks like this:
enter image description here
I would like to have the 'file' and the 'link' field in something like the 'Picture' field...
I updated buildForm :

public function buildForm(array $form, FormStateInterface $form_state)
  {
    $form = parent::buildForm($form, $form_state);
    $entity = $this->entity;
    $form['link']['widget']['0']['#type'] = 'details';
    return $form;
  }

And I got this:
enter image description here

How can I do the same for the 'File' field (have it surrounded in some 'Details')?

  • Why is the File field different? Or do you want to put multiple elements in the same <details> element? Then you need to move the fields to the second level of the $form array inside of a details element. – 4k4 Jun 7 at 13:22
  • I guess File field is different because of its cardinality (when I add ->setCardinality(3) to its BaseFieldDefinition, I get the same 'Details' behavior). All I wanted is to have a common visual aspect for the three fields, whatever their cardinality. Remember that the default form has been generated by Drupal Console. – gbmapo Jun 7 at 16:44
  • @4k4 How can I "move" the field? Let's say I add a 'details' just before with $form['filed'] = array('#type' => 'details', '#title' => t('File')); what am I supposed to do with $form['file']...? – gbmapo Jun 8 at 10:05
  • Move it inside of the details element, see api.drupal.org/api/drupal/… – 4k4 Jun 8 at 10:16
1

I did it this way (thanks to @4K4 to put me on the right direction):

$form['fileDetails'] = array(
  '#type' => 'details',
  '#title' => $this->t('File'),
  '#weight' => 8,
);
$form['file']['#group'] = 'fileDetails';

$form['link']['widget']['0']['#type'] = 'details';

And I get this:
enter image description here

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.