How set a time type field programmatically? I create a custom content entity type.

I want one field for the event time.

Since there is no time field, but a dataTime_type, I create a plugin for a custom field :


  FieldType : TimeItem.php


/**
 * Plugin implementation of the 'time' field type.
 *
 * @FieldType(
 *   id = "time",
 *   label = @Translation("Time Field"),
 *   description = @Translation("Permet la creation d'un champ de type time"),
 *   default_widget = "time_widget",
 *   default_formatter = "time_formatter"
 * )
 */

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
        'columns' =&gt; array(
            'value' =&gt; array(
                'description' =&gt; 'The time value.',
                'type' =&gt; 'int',
                'length' =&gt; 6,
            ),
        ),

    );
  }


I try to change type to time (for mysql) but mysql error return me a null for the type.
So I use int for store time in seconde.


  FieldWidget : TimeWIdget.php


/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
$value = isset($items[$delta]-&gt;value) ? $items[$delta]-&gt;value : '';
$element += array(
'#type' =&gt; 'time', //HTML5 input
'#default_value' =&gt; $value,
'#size' =&gt; 4,
'#element_validate' =&gt; array(
array($this, 'validate'),
),
);

return $element;
}


My entity :

 $fields['heure_evenement'] = BaseFieldDefinition::create('time')
        -&gt;setLabel(t('test'))
        -&gt;setDescription(t('test'))
        -&gt;setRequired(TRUE)
        -&gt;setDefaultValue('')
        -&gt;setDisplayOptions('view', array(
            'label' =&gt; 'above',
            'type' =&gt; 'string',
            'weight' =&gt; 3,
        ))
        -&gt;setDisplayOptions('form', array(
            'weight' =&gt; 3,
        ))
        -&gt;setDisplayConfigurable('form', TRUE)
        -&gt;setDisplayConfigurable('view', TRUE);


All is working except one thing, the HTML5 input type time

Drupal know some input in html5 but not all... type tel, email, number, range, color, date, datetime is ok but not the type time alone.

So I was hoping get it with a custom plugin, but no...

Edit 1

The only way I have found is to create 2 select for this type :

public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
$value = isset($items[$delta]-&gt;value) ? $items[$delta]-&gt;value : '';

    //heure
    for($i=0;$i&lt;=24;$i++){
        $hour[]=$i;
    }

    //minutes
    for($i=0;$i&lt;=59;$i++){
        $minutes[]=$i;
    }


$element += array('hour'=&gt;array(
    '#title'=&gt;t('Hour'),
    '#type' =&gt; 'select',
    '#options'=&gt;$hour,
    '#default_value' =&gt; $value,
    '#element_validate' =&gt; array(
    array($this, 'validate'),
    ),
)
);

    $element += array('minutes'=&gt;array(
        '#title'=&gt;t('Minutes'),
        '#type' =&gt; 'select',
        '#options'=&gt;$minutes,
        '#default_value' =&gt; $value,
        '#element_validate' =&gt; array(
            array($this, 'validate'),
        ),
    )
    );


return $element;
}


Any idea about this?
The form element property #type refers to an element type, not a HTML element. It is okay to define your own element types (or theme types), and you can provide your own markup for it. This can be done by implementing a RenderElement plugin.

Update:

You can also use the datetime render element and disable the date portion.

$element['#date_date_element'] = 'none';

Update 2:

The comment form has an example of a render array that uses the datetime type. So given that as a guideline,

  $form['time_without_date'] = array(
    '#type' =&gt; 'datetime',
    '#title' =&gt; $this-&gt;t('Time without date'),
    // HTML 5 time element format can be H:i
    '#default_value' =&gt; DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 20:00', 'UTC'),
    '#size' =&gt; 20,
    '#date_date_element' =&gt; 'none',
    // This sets html 5 time element use explicitly, probably not necessary.
    '#date_time_element' =&gt; 'time',
  );