Custom action TypeError: array_key_exists

Created on 14 May 2024, about 1 month ago
Updated 16 May 2024, about 1 month ago

Problem/Motivation

I got error with custom action
TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor->processResults() (line 536 of C:\www\d10\web\modules\contrib\views_bulk_operations\src\Service\ViewsBulkOperationsActionProcessor.php).

Steps to reproduce

Create custom module with custom action

Proposed resolution

dont use array_key_exists because $action_definition is not sure array or define in custom action

$output['api_version'] = $action_definition['api_version'] ?? '1';
๐Ÿ› Bug report
Status

Needs review

Version

4.2

Component

Core

Created by

๐Ÿ‡ซ๐Ÿ‡ทFrance lazzyvn paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @lazzyvn
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhishek_virasat

    abhishek_gupta1 โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !82fix the issue โ†’ (Open) created by abhishek_virasat
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.2.x + Environment: PHP 8.2 & MySQL 8
    last update about 1 month ago
    15 pass
  • Status changed to Needs review about 1 month ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhishek_virasat

    @lazzyvn, I have verified above patch. issue has fixed. working for . also I created MR easy to merge. please review it once

  • Pipeline finished with Success
    about 1 month ago
    Total: 613s
    #173204
  • Status changed to Needs work about 1 month ago
  • ๐Ÿ‡ต๐Ÿ‡ฑPoland Graber

    This needs more work with new Drupal\Component\Plugin\Definition\PluginDefinition.. we need to account for that as well probably as it can now be returned by getPluginDefinition().

    Please try:

        // Set the API version.
        $action_definition = $this->actionManager->getDefinition($this->action->getPluginId());
        $output['api_version'] = \array_key_exists('api_version', $action_definition) ? $action_definition['api_version'] : '1';
    

    Instead.

  • First commit to issue fork.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.2.x + Environment: PHP 8.2 & MySQL 8
    last update about 1 month ago
    PHPLint Failed
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.2.x + Environment: PHP 8.2 & MySQL 8
    last update about 1 month ago
    15 pass
  • Status changed to Needs review about 1 month ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance lazzyvn paris

    @Graber
    why i see in the code it uses array_key_exists instead of '??' I think ?? it's faster and safer
    or compatible with old version of php, we can use !empty($array[$key]), this helps to avoid a lot of errors

    For someone who looking for dependency injection in VBO and want to get pre-configuration

    use Drupal\Core\Plugin\PluginBase;
    
    class CustomVBOAction extends ViewsBulkOperationsActionBase implements ViewsBulkOperationsPreconfigurationInterface, ContainerFactoryPluginInterface {
    
      public function __construct($plugin_id, $plugin_definition, $field_definition, protected EntityTypeManagerInterface $entityTypeManager,  protected AccountInterface $currentUser) {
        PluginBase::__construct($field_definition, $plugin_id, $plugin_definition);
      }
    // Execution get error when you need pre-configuration with getConfiguration() it will return NULL. You have to get context instead of getConfiguration()
      public function execute($entity = NULL) {
        if ($entity) {
          $config = $this->getConfiguration();
          if (empty($config)) {
            $config = $this->context['configuration'];
          }
      ............
    } 
    
    
  • ๐Ÿ‡ต๐Ÿ‡ฑPoland Graber

    array_key_exists is strict, we know the argument must be an array, ?? works with everything which may lead to unexpected results. If array_key_exists wasn't used, we wouldn't have this issue and we wouldn't know there are changes in core that could silently break our functionalities and we'd have issues that would be much harder to discover and investigate.
    Strict approach leaves no place for such issues, array must be an array, int must be int etc.

Production build 0.69.0 2024