Score:-1

Argument #1 ($config_factory) must be of type Drupal\Core\Config\ConfigFactoryInterface, array given

cn flag

I'm writing a new module for the community and I have this error and I'm not understand what's going on.

The error:

Mensaje TypeError: Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles::__construct(): Argument #1 ($config_factory) must be of type Drupal\Core\Config\ConfigFactoryInterface, array given, called in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 en Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles->__construct() (línea 20 de /var/www/html/web/modules/custom/view_mode_by_owner_role/src/Functors/ListOfNodeBundles.php)

My functor class:

<?php

namespace Drupal\view_mode_by_owner_role\Functors;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;


final class ListOfNodeBundles implements ContainerInjectionInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, ){
    $this->view_mode_by_onwer_role = $config_factory->get('view_mode_by_owner_role.settings');
    $this->entityTypeManager = $entity_type_manager;
  }

  public static function create (ContainerInterface $container){
    return new static (
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
    );
  }

  public function __invoke(): array {
    $nodes = $this->entityTypeManager->getStorage('node_type')->loadMultiple();

    $bundle_list = [];
    foreach ($nodes as $bundle) {
      $bundle_list[$bundle->id()] = $bundle->label();
    }

    return $bundle_list;
  }
}

My service.yml

services:
  view_mode_by_owner_role.functors_list_of_node_bundles:
    class: Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles
    arguments:
      - ['@config.factory', '@entity_type.manager']

And How I try to implement in a form:

<?php

namespace Drupal\view_mode_by_owner_role\Form;

use Drupal\view_mode_by_owner_role\Form\BasicSettingsForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure View mode by owner role settings for this site.
 */
class SettingsChooseRoleForm extends BasicSettingsForm {

  /**
   * @var \Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles
   */
  private $listOfNodeBundles;

  public function __construct(ListOfNodeBundles $list_of_nodes_bundles){
    parent::__construct($config_factory);
    $this->listOfNodeBundles = $list_of_nodes_bundles;

  }

  public static function create(ContainerInterface $container){
    return new static(
      $container->get('view_mode_by_owner_role.functors_list_of_node_bundles'),
    );
  }
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'view_mode_by_owner_role_settings_choosed_roles';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['view_mode_by_owner_role.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $listado = $this->listOfNodeBundles;
    $role_list = $this->listOfRoles();
    $config = $this->config('view_mode_by_owner_role.settings');

    $form['choosed_roles'] = [
      '#required' => TRUE,
      '#type' => 'checkboxes',
      '#options' => $role_list,
      '#title' => $this->t('Role list'),
      '#default_value' => $config->get('choosed_roles')
    ];

    // Add a submit button that handles the submission of the form.
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
      '#description' => $this->t('Submit'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $config = $this->config('view_mode_by_owner_role.settings')
    ->set('choosed_roles', array_filter($form_state->getValue('choosed_roles')))
    ->save();

    parent::submitForm($form, $form_state);
  }

}

I have read some questions, but I don't understand why I have this error.

cn flag
`parent::__construct($config_factory);` where does that variable come from?
apaderno avatar
us flag
You never use a `ListOfNodeBundles` instance as function, which is when `__invoke()` is called. You should use `$listado = $this->listOfNodeBundles();` not `$listado = $this->listOfNodeBundles;`.
apaderno avatar
us flag
`ListOfNodeBundles` does not need to implement `ContainerInjectionInterface`, nor to have a `create()` method. That does not cause the quoted exception, but it is not how a service class is implemented.
taggartJ avatar
cn flag
also remove the last "," after $entity_type_manager -- from public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, ) <<<-- that will break it too
apaderno avatar
us flag
The trailing comma after the last parameter is only allowed on PHP 8.0. For a module that could run with different PHP versions, it is better to remove that comma.
Score:2
us flag

The error is caused by how the service arguments are defined.

services:
  view_mode_by_owner_role.functors_list_of_node_bundles:
    class: Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles
    arguments:
      - ['@config.factory', '@entity_type.manager']

That service definition is telling Drupal to pass an array as first argument for the class constructor, when its first parameter is type-hinted as Drupal\Core\Config\ConfigFactoryInterface.

The correct definition for that service would be either one or the other of the following definitions.

services:
  view_mode_by_owner_role.functors_list_of_node_bundles:
    class: Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles
    arguments: ['@config.factory', '@entity_type.manager']
services:
  view_mode_by_owner_role.functors_list_of_node_bundles:
    class: Drupal\view_mode_by_owner_role\Functors\ListOfNodeBundles
    arguments:
      - '@config.factory'
      - '@entity_type.manager'
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.