3

PHPStorm, PHP 7.4 IDE highlights $container->get('date.formatter'), with

"Expected parameter of type '\Drupal\Core\Datetime\DateFormatterInterface', 'object' provided"

This function is an exact copy of the core TimestampFormatter (which also shows the error)

/**
 * {@inheritdoc}
 */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  return new static(
    $plugin_id,
    $plugin_definition,
    $configuration['field_definition'],
    $configuration['settings'],
    $configuration['label'],
    $configuration['view_mode'],
    $configuration['third_party_settings'],
    $container->get('date.formatter'),
    $container->get('entity_type.manager')->getStorage('date_format')
  );
}

The __construct is looking for a DateFormatterInterface (see below) and not sure how this works in core, but my "viewElements" is never firing since this error started.

public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, **DateFormatterInterface $date_formatter**, EntityStorageInterface $date_format_storage) {

What is the proper container->get to use to return a proper date.formatter and not an object which causes the create to fail without any errors logged that I can see?

1 Answer 1

3

Your code is fine, but you can remove the warnings by installing the PHPStorm Symfony plugin.

It teaches the IDE how to resolve the correct class from the service name you provide to $container->get().

3
  • Thanks @Clive as I've seen you answer thousands of questions over many years. Kudos. Strange thing is I have my field set to format based on this plugin but it is not called anymore when the node is viewed. It was working some time ago but not now. I have breakpoints set in the constructor and the viewElements but they never fire.
    – Scott
    Commented Nov 2, 2021 at 21:04
  • Adjusted IDE and warning did go away. I guess I misunderstand how the formatter works for a field when editing a node. My dateTime field format must change based on a condition. Does the formatter not interact with the widget that displays the date? Should I ask this as a separate question?
    – Scott
    Commented Nov 2, 2021 at 21:23
  • Don't forget to the 'Symfony' plugin :-). Commented May 5, 2023 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.