setRelationship example

public function testSetRelationship() {
    $view = Views::getView('test_handler_relationships');
    $view->setDisplay();
    // Setup a broken relationship.     $view->addHandler('default', 'relationship', $this->randomMachineName()$this->randomMachineName()[], 'broken_relationship');
    // Setup a valid relationship.     $view->addHandler('default', 'relationship', 'comment_field_data', 'node', ['relationship' => 'cid'], 'valid_relationship');
    $view->initHandlers();
    $field = $view->field['title'];

    $field->options['relationship'] = NULL;
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');

    $field->options['relationship'] = $this->randomMachineName();
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');

    $field->options['relationship'] = 'broken_relationship';
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that a broken relationship does not create a relationship on the field.');

    $field->options['relationship'] = 'valid_relationship';
    
    $title = $this->display_handler->getOption('title');

    // Iterate through each argument and process.     foreach ($this->argument as $id => $arg) {
      $position++;
      $argument = $this->argument[$id];

      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;
          }
          
// Make sure the original order of sorts is preserved       // (e.g. a sticky sort is often first)       $view->query->orderby = [];
      foreach ($view->sort as $sort) {
        if (!$sort->isExposed()) {
          $sort->query();
        }
        elseif (!empty($sort->options['expose']['field_identifier']) && $sort->options['expose']['field_identifier'] === $sort_by) {
          if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order']['ASC', 'DESC'], TRUE)) {
            $sort->options['order'] = $exposed_data['sort_order'];
          }
          $sort->setRelationship();
          $sort->query();
        }
      }
    }
  }

  /** * {@inheritdoc} */
  public function preRender($values) {}

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