1

For example, there is a situation, where I need to output date field in some another format. But this format is not supported by PHP.

For example, I want to output year value in Roman numerals format. Or, I want to calculate year not from the Christmas, but from "the Creation of the world", from foundation of Rome ans so on. Or "the week" in my calendar doesn't contain 7 days, but it contains another quantity of them.

In this case, if I'm not mistaken, I should create new class inheriting it from DateTime class and there redefine method format or do something else.

Can I afterwards notice Drupal to it use my own DateTime class instead default ? That is, for instance, I have created new DateTime field for some node type and I can set my date format for this field in the admin date settings page, in Views and so on.

Is it possible in Drupal 7 ? And is it possible in Drupal 8 ? If yes, how to do it ?

3

In your case, you do not need to define a new field type, you just need to write a new field formatter.

In Drupal 8, field formatters are used for displaying fields when displaying the parent entity (eg, a node view page) and when used in a view (even with field output).

Field formatters are easy to write in Drupal 8. In you case you just create a new plugin, and you can likely just extend one of the existing ones. I suspect something like this would work for you

namespace Drupal\mymodule\Plugin\Field\FieldFormatter;

use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimePlainFormatter;

/**
 * Plugin implementation of the 'Awesome' formatter for 'datetime' fields.
 *
 * @FieldFormatter(
 *   id = "mymodule_datetime_awesome",
 *   label = @Translation("Awesome"),
 *   field_types = {
 *     "datetime"
 *   }
 *)
 */
class AwesomeDateTimeFormatter extends DateTimePlainFormatter {

  /**
   * {@inheritdoc}
   */
  protected function formatDate($date) {
    // Do something awesome!
  }

}

Here, the DateTimePlainFormatter does all of the heavy lifting via inheritance and you just override the method that actually produces the date string to be whatever you need. You can then use this on Manage Field Display, and other places you can choose a field formatter.

In Drupal 7, you can also just create a field formatter, but the process is more involved. You would also need to create a views field output plugin, as Drupal 7 / Views 3 does not use field formatters with field output.

1

In Drupal 8 you can define any field as plugin. You can create your own custom field type i.e roman_date_type and if you want custom widget than roman_field_widget and than finally formatter roman_field_formatter where you can format your date. If you want existing field type like date_time than prob. you only need custom formatter. To tell Drupal 8 about your custom plugin (roman_field_formatter) you need to put your custom class in (YOUR_MODULE\Plugin\Field\FieldFormatter\YOUR_CUSTOME_CLASS) as Drupal 8 uses PSR-4 standard. For reference you can see datetime module in core. I hope it will answer your question.

  • Maybe, in this case it would be easily to use module Custom Formatters ? Here one needn't to write a module for it. – Yakimkin Roman Jan 10 '17 at 10:43

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.