0

I'm creating new Field Types in Drupal 8. I recently asked which are the type names allowed for the DataDefinition::create() method, receiving a good answer.

Now I figured out that I need at least other two type names:

  • A column type name to use inside the method schema(...) from the FieldItemBase class.
  • Another type name to create elements inside the method formElement(...) from the class WidgetBase.

For example this is a snippet of code used to create a new Field Type, defining the schema() method:

public static function schema(FieldStorageDefinitionInterface $fd) {

  $columns = array(

    // 

    // Here the column type name is 'varchar'
    'password' => array(
      'description' => 'A plain text password.',
      'type' => 'varchar',
    ),

    // 

  );

  $schema = array(
    'columns' => $columns,
    'indexes' => array(),
    'foreign keys' => array(),
  );

  return $schema;
}

In the above code the column type name is 'varchar'.

And this is another snippet, defining the formElement method (always used defining the Field Type):

public function formElement(
  FieldItemListInterface $items, 
  $delta, 
  array $element, 
  array &$form, 
  FormStateInterface $form_state
) {

  //

  // Here the element type name is 'password'
  $element['passwordlist'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Select a password for the user'),
  );

  //

  return $element;
}

In the above code the element type name is 'password'.

What can I use for 'type' in the first case and '#type' in the second case?

1

All available schema types are listed in the following table: https://www.drupal.org/node/159605

Here is a list of available types of render elements (first column of the table corresponds to the #type property of the element) https://api.drupal.org/api/drupal/elements

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.