I am writing a custom module, I am trying to use the validateExtensions() method defined from the Drupal core File module inside the FileItem.php file, in my own controller, using the following code.

use Drupal\file\Plugin\Field\FieldType\FileItem;

//Some other Use statement 

class someclass extends FormBase {
  $instance = new FileItem();
  //Some Code
  '#element_validate' => array(array(get_class($instance), 'validateExtensions')),

It gives the following error:

Recoverable fatal error: Argument 1 passed to Drupal\Core\Field\FieldItemBase::__construct() must be an instance of Drupal\Core\TypedData\DataDefinitionInterface, none given


The answer is in the error message:

FieldItemBase has a constructor, and it waits a parameter (actually multiple), but it doesn't get any:

$instance = new FileItem();

Even though the constructor function can't be find in FileItem, in this case you have to investigate which parent has it. You will find out that "FieldItemBase" has it. I don't know why you want to use this like this, but things like Controllers, Plugins, Services were mean to be to get instantiated by Drupal.

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.