1

I'm trying to create a custom FieldType which provides the fields for a complete address (street, house_number, post_code and city).

I created a FieldWidget, FieldFormatter and FieldType class and a "field"-template. (followed the guide at http://www.computerminds.co.uk/drupal-code/drupal-8-creating-custom-field-part-1-field-type and "seeing" Drupal core FieldTypes)

The widget does not get rendered unless I add an element_info()-Hook and a address_preprocess-Hook where I have to define the same stuff like in the FildWidget::formElement() method. But this is the Drupal7 way. Am I missing something?

FieldWidget class

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
  $element             = $element + array(
    'street'    => array(
      '#title'            => $this->t('Street'),
      '#type'             => 'textfield',
      '#default_value'    => isset($items[$delta]->street) ? $items[$delta]->street : null,
      '#required'         => false,

    ),
    'street_nr' => array(
      ...
    ),
    'zip'       => array(
      ...
    ),
    'city'      => array(
      ...
    ),
  );
  $form['#validate'][] = array($this, 'validateCompleteAddress');

  return $element;
}

FieldFormater class

public function viewElements(FieldItemListInterface $items)
{
  $elements = array();

  $elements['addresses'] = array(
    '#type' => 'fieldset',
    '#tree' => true,
  );

  foreach ($items as $delta => $item)
  {
    $elements['addresses'][$delta] = array(
      '#theme'     => 'fields_address',
      'elements' => $item,

      //'street'    => String::checkPlain($item->street),
      //'street_nr' => String::checkPlain($item->street_nr),
      //'zip'       => String::checkPlain($item->zip),
      //'city'      => String::checkPlain($item->city),
    );
  }

FieldType class

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition)
{
  $properties = array(
    'street' => DataDefinition::create('string')->setLabel(t('Street'))->addConstraint('Length', array('max' => 255)),
    'street_nr' => DataDefinition::create('string')->setLabel(t('Street number'))->addConstraint('Length', array('max' => 100)),
    'zip' => DataDefinition::create('string')->setLabel(t('Zip'))->addConstraint('Length', array('max' => 5)),
    'city' => DataDefinition::create('string')->setLabel(t('City'))->addConstraint('Length', array('max' => 255)),
  );

  return $properties;
}

public static function schema(FieldStorageDefinitionInterface $field_definition)
{
  return array(
    'columns' => array(
      'street'    => array(
        ...
      ),
      'street_nr' => array(
        ...
      ),
      'zip'       => array(
        ...
      ),
      'city'      => array(
        ...
    ),

  ),
  'indexes' => array(),
  'foreign keys' => array(),
);

}

  • This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. See Help Center. – Mołot Sep 10 '14 at 7:08
  • added some code of the classes – Seb Sep 10 '14 at 7:26
  • It is not clear what exactly you are asking. There is nothing special about what viewElements() returns and what you can do anywhere else with render arrays. If you use theme functions or special #type's, then you need to define them yourself. There is no magic. – Berdir Sep 11 '14 at 17:20
  • It was not clear to me, that one has to define the same stuff twice. (backend and frontend) – Seb Sep 15 '14 at 11:46
  • Seems like one has to provide a custom "Element" class in module\Element folder to define custom Input elements for the form rendering: @see \Drupal\Core\Render\Element\Textfield – Seb Aug 21 '15 at 13:15
-1

It seems like I have to provide a custom Element class in the module\Element folder to define the custom input elements for the form rendering.

See \Drupal\Core\Render\Element\Textfield.

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.