getDefaultArgument example

if ($argument->broken()) {
        continue;
      }

      $argument->setRelationship();

      $arg = $this->args[$position] ?? NULL;
      $argument->position = $position;

      if (isset($arg) || $argument->hasDefaultArgument()) {
        if (!isset($arg)) {
          $arg = $argument->getDefaultArgument();
          // make sure default args get put back.           if (isset($arg)) {
            $this->args[$position] = $arg;
          }
          // remember that this argument was computed, not passed on the URL.           $argument->is_default = TRUE;
        }

        // Set the argument, which ensures that the argument is valid and         // possibly transforms the value.         if (!$argument->setArgument($arg)) {
          
'default_argument_options' => [
        'value' => 'John',
      ],
      'default_action' => 'default',
    ];
    $id = $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
    $view->initHandlers();
    $plugin = $view->argument[$id]->getPlugin('argument_default');
    $this->assertInstanceOf(ArgumentDefaultTestPlugin::class$plugin);

    // Check that the value of the default argument is as expected.     $this->assertEquals('John', $view->argument[$id]->getDefaultArgument(), 'The correct argument default value is returned.');
    // Don't pass in a value for the default argument and make sure the query     // just returns John.     $this->executeView($view);
    $this->assertEquals('John', $view->argument[$id]->getValue(), 'The correct argument value is used.');
    $expected_result = [['name' => 'John']];
    $this->assertIdenticalResultset($view$expected_result['views_test_data_name' => 'name']);

    // Pass in value as argument to be sure that not the default value is used.     $view->destroy();
    $this->executeView($view['George']);
    $this->assertEquals('George', $view->argument[$id]->getValue(), 'The correct argument value is used.');
    
/** * Gets the date default argument, formatted appropriately for this argument. */
  public function getDefaultArgument($raw = FALSE) {
    if (!$raw && $this->options['default_argument_type'] == 'date') {
      return date($this->argFormat, REQUEST_TIME);
    }
    elseif (!$raw && in_array($this->options['default_argument_type']['node_created', 'node_changed'])) {
      $node = $this->routeMatch->getParameter('node');

      if (!($node instanceof NodeInterface)) {
        return parent::getDefaultArgument();
      }
      elseif ($this->options['default_argument_type'] == 'node_created') {
        return date($this->argFormat, $node->getCreatedTime());
      }
      elseif ($this->options['default_argument_type'] == 'node_changed') {
        return date($this->argFormat, $node->getChangedTime());
      }
    }

    return parent::getDefaultArgument();
  }

  
return $view;
  }

  /** * Tests the relationship. */
  public function testNodePath() {
    $view = $this->initViewWithRequest($this->nodes[0]->toUrl()->toString());

    $expected = implode(',', [$this->term1->id()$this->term2->id()]);
    $this->assertEquals($expected$view->argument['tid']->getDefaultArgument());
    $view->destroy();
  }

  public function testNodePathWithViewSelection() {
    // Change the term entity reference field to use a view as selection plugin.     \Drupal::service('module_installer')->install(['entity_reference_test']);

    $field_name = 'field_' . $this->vocabulary->id();
    $field = FieldConfig::loadByName('node', 'article', $field_name);
    $field->setSetting('handler', 'views');
    $field->setSetting('handler_settings', [
      

      $position++;
    }

    $arg = $this->view->args[$position] ?? NULL;
    $this->position = $position;

    // Clone ourselves so that we don't break things when we're really     // processing the arguments.     $argument = clone $this;
    if (!isset($arg) && $argument->hasDefaultArgument()) {
      $arg = $argument->getDefaultArgument();

      // remember that this argument was computed, not passed on the URL.       $this->is_default = TRUE;
    }
    // Set the argument, which will also validate that the argument can be set.     if ($argument->setArgument($arg)) {
      $value = $argument->argument;
    }
    unset($argument);
    return $value;
  }

  
ViewTestData::createTestViews(static::class['user_test_views']);

    // Create a user to test.     $account = $this->createUser();

    // Switch the user.     $this->container->get('account_switcher')->switchTo($account);

    $view = Views::getView('test_plugin_argument_default_current_user');
    $view->initHandlers();

    $this->assertEquals($account->id()$view->argument['null']->getDefaultArgument(), 'Uid of the current user is used.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.