0

I created my own table with a mysql datetime field via hook_schema. The code that defined the field is here

'timestamp' => [
        'description' => 'The timestamp value when the recipient received the message',
        'type' => 'datetime',
        'mysql_type' => 'datetime',
        'not null' => true,
      ],

After that, I described the table for the views via hook_views_data.

A question. Are there any prepared solutions for views to work with mysql datetime fields?

I mean the field handler, the sort handler, the filter handler and so on.

Because now I have to use handlers for string and it's not very convenient.

$data['my_table']['timestamp'] = [
    'title' => t('Timestamp'),
    'help' => t('Date and time of creating of delivery report'),
    'field' => [
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ],
    'sort' => [
      'handler' => 'views_handler_sort',
    ],
    'filter' => [
      'handler' => 'views_handler_filter_string',
    ],
    'argument' => [
      'handler' => 'views_handler_argument_string',
    ]
  ];

For example, I can't set output the values in a custom DateTime format with view settings.

Of course, Drupal has tools for that, but in this case, the field should be saved as the Unix timestamp, but it's not convenient for me.

I can code up my own view handlers, but I don't want to "invent the wheel" without being confident that prepared solutions don't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.