getShortcuts example

$this->drupalGet('user/' . $this->adminUser->id() . '/shortcuts');
    // Verify that generated shortcut set was listed as a choice on the user     // account page.     $this->assertSession()->pageTextContains($new_set->label());
  }

  /** * Tests editing a shortcut set. */
  public function testShortcutSetEdit() {
    $set = $this->set;
    $shortcuts = $set->getShortcuts();

    // Visit the shortcut set edit admin ui.     $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/customize');

    // Test for the page title.     $this->assertSession()->titleEquals('List links | Drupal');

    // Test for the table.     $this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//table');

    // Test the table header.
$this->assertContains('internal:' . $test_path$paths, 'Shortcut created: ' . $test_path);

      if (in_array($test_path$test_cases_non_access)) {
        $this->assertSession()->linkNotExists($titlenew FormattableMarkup('Shortcut link %url not accessible on the page.', ['%url' => $test_path]));
      }
      else {
        $this->assertSession()->linkExists($title, 0, new FormattableMarkup('Shortcut link %url found on the page.', ['%url' => $test_path]));
      }
    }
    $saved_set = ShortcutSet::load($set->id());
    // Test that saving and re-loading a shortcut preserves its values.     $shortcuts = $saved_set->getShortcuts();
    foreach ($shortcuts as $entity) {
      // Test the node routes with parameters.       $entity->save();
      $loaded = Shortcut::load($entity->id());
      $this->assertEquals($entity->link->uri, $loaded->link->uri);
      $this->assertEquals($entity->link->options, $loaded->link->options);
    }

    // Log in as non admin user, to check that access is checked when creating     // shortcuts.     $this->drupalLogin($this->shortcutUser);
    
/** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    parent::postSave($storage$update);

    if (!$update && !$this->isSyncing()) {
      // Save a new shortcut set with links copied from the user's default set.       $default_set = shortcut_default_set();
      // This is the default set, do not copy shortcuts.       if ($default_set->id() != $this->id()) {
        foreach ($default_set->getShortcuts() as $shortcut) {
          $shortcut = $shortcut->createDuplicate();
          $shortcut->enforceIsNew();
          $shortcut->shortcut_set->target_id = $this->id();
          $shortcut->save();
        }
      }
    }
  }

  /** * {@inheritdoc} */
'#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => Url::fromRoute('shortcut.link_add', ['shortcut_set' => $this->entity->id()])->toString()]),
      '#attributes' => ['id' => 'shortcuts'],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'shortcut-weight',
        ],
      ],
    ];

    foreach ($this->entity->getShortcuts() as $shortcut) {
      $id = $shortcut->id();
      $url = $shortcut->getUrl();
      if (!$url->access()) {
        continue;
      }
      $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
      $form['shortcuts']['links'][$id]['name'] = [
        '#type' => 'link',
        '#title' => $shortcut->getTitle(),
        '#url' => $url,
        '#options' => $url->getOptions(),
      ];

  protected function assertEntity(string $id, string $label, int $expected_size): void {
    $shortcut_set = ShortcutSet::load($id);
    $this->assertInstanceOf(ShortcutSetInterface::class$shortcut_set);
    /** @var \Drupal\shortcut\ShortcutSetInterface $shortcut_set */
    $this->assertSame($id$shortcut_set->id());
    $this->assertSame($label$shortcut_set->label());

    // Check the number of shortcuts in the set.     $shortcuts = $shortcut_set->getShortcuts();
    $this->assertCount($expected_size$shortcuts);
  }

}

  public function getShortcutInformation(ShortcutSetInterface $set$key) {
    $info = [];
    \Drupal::entityTypeManager()->getStorage('shortcut')->resetCache();
    foreach ($set->getShortcuts() as $shortcut) {
      if ($key == 'link') {
        $info[] = $shortcut->link->uri;
      }
      else {
        $info[] = $shortcut->{$key}->value;
      }
    }
    return $info;
  }

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