/** * Gets a rendered link from an url object. * * @param string $text * The link text for the anchor tag as a translated string. * @param \Drupal\Core\Url|string $url * The URL object or string used for the link. * * @return string * An HTML string containing a link to the given url. */ public function getLink($text, $url) { if (!$url instanceof Url) { $url = Url::fromUri($url); } return $this->linkGenerator->generate($text, $url); }
/** * Returns the list of consumers for a user. * * @param \Drupal\user\UserInterface $user * A user account object. * * @return string * A HTML-formatted string with the list of OAuth consumers. */ public function consumers(UserInterface $user) { $list = array(); $list['#cache']['tags'] = array('oauth:' => $user->id()); $list['heading']['#markup'] = $this->linkGenerator->generate($this->t('Add consumer'), Url::fromRoute('oauth.user_consumer_add', array('user' => $user->id()))); // Get the list of consumers. $result = $this->user_data->get('oauth', $user->id()); // Define table headers. $list['table'] = array('#theme' => 'table', '#header' => array('consumer_key' => array('data' => $this->t('Consumer key')), 'consumer_secret' => array('data' => $this->t('Consumer secret')), 'operations' => array('data' => $this->t('Operations'))), '#rows' => array()); // Add existing consumers to the table. foreach ($result as $key => $consumer) { $list['table']['#rows'][] = array('data' => array('consumer_key' => $key, 'consumer_secret' => $consumer['consumer_secret'], 'operations' => array('data' => array('#type' => 'operations', '#links' => array('delete' => array('title' => $this->t('Delete'), 'url' => Url::fromRoute('oauth.user_consumer_delete', array('user' => $user->id(), 'key' => $key)))))))); } $list['table']['#empty'] = $this->t('There are no OAuth consumers.'); return $list; }
/** * @covers ::validateCurrencyNumber * @dataProvider providerTestValidateCurrencyNumber */ public function testValidateCurrencyNumber($valid, $currency_number, $currency_is_new, $currency_number_exists = FALSE) { $element = array('#value' => $currency_number); $form = array(); $form_state = $this->getMock(FormStateInterface::class); $this->currency->expects($this->any())->method('isNew')->willReturn($currency_is_new); if (!$valid) { $form_state->expects($this->once())->method('setError')->with($element, 'The currency number must be three digits.'); } elseif ($currency_number_exists) { $loaded_currency_code = $this->randomMachineName(); $loaded_currency_label = $this->randomMachineName(); $loaded_currency_url = new Url($this->randomMachineName()); $loaded_currency = $this->getMock(CurrencyInterface::class); $loaded_currency->expects($this->any())->method('id')->willReturn($loaded_currency_code); $loaded_currency->expects($this->any())->method('label')->willReturn($loaded_currency_label); $loaded_currency->expects($this->atLeastOnce())->method('urlInfo')->willReturn($loaded_currency_url); $this->currencyStorage->expects($this->once())->method('loadByProperties')->with(array('currencyNumber' => $currency_number))->willReturn(array($loaded_currency)); $form_state->expects($this->once())->method('setError'); $this->linkGenerator->expects($this->once())->method('generate')->with($loaded_currency_label, $loaded_currency_url); } else { $this->currencyStorage->expects($this->once())->method('loadByProperties')->with(array('currencyNumber' => $currency_number))->willReturn(FALSE); $form_state->expects($this->never())->method('setError'); $form_state->expects($this->never())->method('setErrorByName'); } $this->sut->validateCurrencyNumber($element, $form_state, $form); }
/** * Utility to build a Juicebox gallery based on field formatter data. * * @param Drupal\juicebox\JuiceboxGalleryInterface $gallery * An initialized Juicebox gallery object. * @param Drupal\Core\Field\FieldItemListInterface $items * A list of field items that contain file data for the gallery. */ protected function buildGallery(JuiceboxGalleryInterface $gallery, FieldItemListInterface $items) { // Get settings. $settings = $this->getSettings(); // Iterate over items and extract image data. foreach ($items as $delta => $item) { if ($item->isDisplayed() && !empty($item->target_id)) { // Calculate the source data that Juicebox requires. $src_data = $this->juicebox->styleImageSrcData($item->entity, $settings['image_style'], $item->entity, $settings['thumb_style'], $settings); // Short-circut this iteration if skipping an incompatible file. if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'skip') { continue; } // Set the image title. If we have an incompatible file and are // configured to show a link, set the title text as the link. if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'show_icon_and_link') { $anchor = !empty($item->description) ? $item->description : $item->entity->get('filename')->value; $title = $this->linkGenerator->generate($anchor, Url::fromUri($src_data['linkURL'])); } else { $title = $this->getFieldText($item, $settings['title_source']); } // Set the image caption. $caption = $this->getFieldText($item, $settings['caption_source']); // Add this image to the gallery. $gallery->addImage($src_data, $title, $caption); } } // Run common build tasks. This is also where the general settings are // applied. $this->juicebox->runCommonBuild($gallery, $settings, $items); }
/** * Recursive helper function for buildOverviewForm(). * * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree * The tree retrieved by \Drupal\Core\Menu\MenuLinkTreeInterface::load(). * @param int $delta * The default number of menu items used in the menu weight selector is 50. * * @return array * The overview tree form. */ protected function buildOverviewTreeForm($tree, $delta) { $form =& $this->overviewTreeForm; $tree_access_cacheability = new CacheableMetadata(); foreach ($tree as $element) { $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access)); // Only render accessible links. if (!$element->access->isAllowed()) { continue; } /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ $link = $element->link; if ($link) { $id = 'menu_plugin_id:' . $link->getPluginId(); $form[$id]['#item'] = $element; $form[$id]['#attributes'] = $link->isEnabled() ? array('class' => array('menu-enabled')) : array('class' => array('menu-disabled')); $form[$id]['title']['#markup'] = $this->linkGenerator->generate($link->getTitle(), $link->getUrlObject()); if (!$link->isEnabled()) { $form[$id]['title']['#markup'] .= ' (' . $this->t('disabled') . ')'; } elseif (($url = $link->getUrlObject()) && $url->isRouted() && $url->getRouteName() == 'user.page') { $form[$id]['title']['#markup'] .= ' (' . $this->t('logged in users only') . ')'; } $form[$id]['enabled'] = array('#type' => 'checkbox', '#title' => $this->t('Enable @title menu link', array('@title' => $link->getTitle())), '#title_display' => 'invisible', '#default_value' => $link->isEnabled()); $form[$id]['weight'] = array('#type' => 'weight', '#delta' => $delta, '#default_value' => $link->getWeight(), '#title' => $this->t('Weight for @title', array('@title' => $link->getTitle())), '#title_display' => 'invisible'); $form[$id]['id'] = array('#type' => 'hidden', '#value' => $link->getPluginId()); $form[$id]['parent'] = array('#type' => 'hidden', '#default_value' => $link->getParent()); // Build a list of operations. $operations = array(); $operations['edit'] = array('title' => $this->t('Edit')); // Allow for a custom edit link per plugin. $edit_route = $link->getEditRoute(); if ($edit_route) { $operations['edit']['url'] = $edit_route; // Bring the user back to the menu overview. $operations['edit']['query'] = $this->getDestinationArray(); } else { // Fall back to the standard edit link. $operations['edit'] += array('url' => Url::fromRoute('menu_ui.link_edit', ['menu_link_plugin' => $link->getPluginId()])); } // Links can either be reset or deleted, not both. if ($link->isResettable()) { $operations['reset'] = array('title' => $this->t('Reset'), 'url' => Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $link->getPluginId()])); } elseif ($delete_link = $link->getDeleteRoute()) { $operations['delete']['url'] = $delete_link; $operations['delete']['query'] = $this->getDestinationArray(); $operations['delete']['title'] = $this->t('Delete'); } if ($link->isTranslatable()) { $operations['translate'] = array('title' => $this->t('Translate'), 'url' => $link->getTranslateRoute()); } $form[$id]['operations'] = array('#type' => 'operations', '#links' => $operations); } if ($element->subtree) { $this->buildOverviewTreeForm($element->subtree, $delta); } } $tree_access_cacheability->merge(CacheableMetadata::createFromRenderArray($form))->applyTo($form); return $form; }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $image_styles = image_style_options(FALSE); $element['image_style'] = array('#title' => t('Image style'), '#type' => 'select', '#default_value' => $this->getSetting('image_style'), '#empty_option' => t('None (original image)'), '#options' => $image_styles, '#description' => array('#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')), '#access' => $this->currentUser->hasPermission('administer image styles'))); $link_types = array('content' => t('Content'), 'file' => t('File')); $element['image_link'] = array('#title' => t('Link image to'), '#type' => 'select', '#default_value' => $this->getSetting('image_link'), '#empty_option' => t('Nothing'), '#options' => $link_types); return $element; }
/** * @covers ::link * * @dataProvider providerTestLink */ public function testLink($entity_label, $link_text, $expected_text, $link_rel = 'canonical', array $link_options = []) { $route_name_map = ['canonical' => 'entity.test_entity_type.canonical', 'edit-form' => 'entity.test_entity_type.edit_form']; $route_name = $route_name_map[$link_rel]; $entity_id = 'test_entity_id'; $entity_type_id = 'test_entity_type'; $expected = '<a href="/test_entity_type/test_entity_id">' . $expected_text . '</a>'; $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity_type->expects($this->once())->method('getLinkTemplates')->willReturn($route_name_map); $entity_type->expects($this->any())->method('getKey')->with('label')->willReturn('label'); $this->entityManager->expects($this->any())->method('getDefinition')->with($entity_type_id)->will($this->returnValue($entity_type)); /** @var \Drupal\Core\Entity\Entity $entity */ $entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', [['id' => $entity_id, 'label' => $entity_label], $entity_type_id]); $expected_link = Link::createFromRoute($expected_text, $route_name, [$entity_type_id => $entity_id], ['entity_type' => $entity_type_id, 'entity' => $entity] + $link_options)->setLinkGenerator($this->linkGenerator); $this->linkGenerator->expects($this->once())->method('generateFromLink')->with($this->equalTo($expected_link))->willReturn($expected); $this->assertSame($expected, $entity->link($link_text, $link_rel, $link_options)); }
/** * Gets a rendered link from an url object. * * @param string $text * The link text for the anchor tag as a translated string. * @param \Drupal\Core\Url|string $url * The URL object or string used for the link. * * @return string * An HTML string containing a link to the given url. */ public function getLink($text, $url) { if ($url instanceof Url) { return $this->linkGenerator->generate($text, $url); } else { // @todo Convert once https://www.drupal.org/node/2306901 is in return _l($text, $url); } }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $element['url_type'] = array('#title' => t('URL type'), '#type' => 'select', '#options' => array(0 => t('Full URL'), 1 => t('Absolute file path'), 2 => t('Relative file path')), '#default_value' => $this->getSetting('url_type')); //$element['url_type'][0]['#description'] = t("Like: 'http://example.com/sites/default/files/image.png'"); //$element['url_type'][1]['#description'] = t("With leading slash, no base URL, like: '/sites/default/files/image.png'"); //$element['url_type'][2]['#description'] = t("No base URL or leading slash, like: 'sites/default/files/image.png'"); $image_styles = image_style_options(FALSE); $element['image_style'] = array('#title' => t('Image style'), '#type' => 'select', '#default_value' => $this->getSetting('image_style'), '#empty_option' => t('None (original image)'), '#options' => $image_styles, '#description' => array('#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')), '#access' => $this->currentUser->hasPermission('administer image styles'))); $link_types = array('content' => t('Content'), 'file' => t('File')); $element['image_link'] = array('#title' => t('Link image to'), '#type' => 'select', '#default_value' => $this->getSetting('image_link'), '#empty_option' => t('Nothing'), '#options' => $link_types); return $element; }
/** * Recursive helper function for buildOverviewForm(). * * @param $tree * The tree retrieved by \Drupal\Core\Menu\MenuLinkTreeInterface::load(). * @param $delta * The default number of menu items used in the menu weight selector is 50. * * @return array * The overview tree form. */ protected function buildOverviewTreeForm($tree, $delta) { $form =& $this->overviewTreeForm; foreach ($tree as $element) { /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ $link = $element->link; if ($link) { $id = 'menu_plugin_id:' . $link->getPluginId(); $form[$id]['#item'] = $element; $form[$id]['#attributes'] = $link->isHidden() ? array('class' => array('menu-disabled')) : array('class' => array('menu-enabled')); $form[$id]['title']['#markup'] = $this->linkGenerator->generateFromUrl($link->getTitle(), $link->getUrlObject(), $link->getOptions()); if ($link->isHidden()) { $form[$id]['title']['#markup'] .= ' (' . $this->t('disabled') . ')'; } elseif (($url = $link->getUrlObject()) && !$url->isExternal() && $url->getRouteName() == 'user.page') { $form[$id]['title']['#markup'] .= ' (' . $this->t('logged in users only') . ')'; } $form[$id]['enabled'] = array('#type' => 'checkbox', '#title' => $this->t('Enable @title menu link', array('@title' => $link->getTitle())), '#title_display' => 'invisible', '#default_value' => !$link->isHidden()); $form[$id]['weight'] = array('#type' => 'weight', '#delta' => $delta, '#default_value' => $link->getWeight(), '#title' => $this->t('Weight for @title', array('@title' => $link->getTitle())), '#title_display' => 'invisible'); $form[$id]['id'] = array('#type' => 'hidden', '#value' => $link->getPluginId()); $form[$id]['parent'] = array('#type' => 'hidden', '#default_value' => $link->getParent()); // Build a list of operations. $operations = array(); $operations['edit'] = array('title' => $this->t('Edit')); // Allow for a custom edit link per plugin. $edit_route = $link->getEditRoute(); if ($edit_route) { $operations['edit'] += $edit_route; // Bring the user back to the menu overview. $operations['edit']['query']['destination'] = $this->entity->url(); } else { // Fall back to the standard edit link. $operations['edit'] += array('route_name' => 'menu_ui.link_edit', 'route_parameters' => array('menu_link_plugin' => $link->getPluginId())); } // Links can either be reset or deleted, not both. if ($link->isResettable()) { $operations['reset'] = array('title' => $this->t('Reset'), 'route_name' => 'menu_ui.link_reset', 'route_parameters' => array('menu_link_plugin' => $link->getPluginId())); } elseif ($delete_link = $link->getDeleteRoute()) { $operations['delete'] = $delete_link; $operations['delete']['query']['destination'] = $this->entity->url(); $operations['delete']['title'] = $this->t('Delete'); } if ($link->isTranslatable()) { $operations['translate'] = array('title' => $this->t('Translate')) + (array) $link->getTranslateRoute(); } $form[$id]['operations'] = array('#type' => 'operations', '#links' => $operations); } if ($element->subtree) { $this->buildOverviewTreeForm($element->subtree, $delta); } } return $form; }
/** * Implements #element_validate for the currency number element. */ public function validateCurrencyNumber(array $element, FormStateInterface $form_state, array $form) { $currency = $this->getEntity(); $currency_number = $element['#value']; if ($currency_number && !preg_match('/^\\d{3}$/i', $currency_number)) { $form_state->setError($element, $this->t('The currency number must be three digits.')); } elseif ($currency->isNew()) { $loaded_currencies = $this->currencyStorage->loadByProperties(array('currencyNumber' => $currency_number)); if ($loaded_currencies) { $loaded_currency = reset($loaded_currencies); $form_state->setError($element, $this->t('The currency number is already in use by @link.', array('@link' => $this->linkGenerator->generate($loaded_currency->label(), $loaded_currency->urlInfo('edit-form'))))); } } }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $responsive_image_options = array(); $responsive_image_styles = $this->responsiveImageStyleStorage->loadMultiple(); if ($responsive_image_styles && !empty($responsive_image_styles)) { foreach ($responsive_image_styles as $machine_name => $responsive_image_style) { if ($responsive_image_style->hasImageStyleMappings()) { $responsive_image_options[$machine_name] = $responsive_image_style->label(); } } } $elements['responsive_image_style'] = array('#title' => t('Responsive image style'), '#type' => 'select', '#default_value' => $this->getSetting('responsive_image_style'), '#required' => TRUE, '#options' => $responsive_image_options, '#description' => array('#markup' => $this->linkGenerator->generate($this->t('Configure Responsive Image Styles'), new Url('entity.responsive_image_style.collection')), '#access' => $this->currentUser->hasPermission('administer responsive image styles'))); $link_types = array('content' => t('Content'), 'file' => t('File')); $elements['image_link'] = array('#title' => t('Link image to'), '#type' => 'select', '#default_value' => $this->getSetting('image_link'), '#empty_option' => t('Nothing'), '#options' => $link_types); return $elements; }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $image_styles = image_style_options(FALSE); $element['image_style'] = array( '#title' => t('Image style'), '#type' => 'select', '#default_value' => $this->getSetting('image_style'), '#empty_option' => t('None (original image)'), '#options' => $image_styles, '#description' => array( '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')), '#access' => $this->currentUser->hasPermission('administer image styles'), ), ); $element['image_link_style'] = array( '#title' => t('Link to image style'), '#type' => 'select', '#default_value' => $this->getSetting('image_link_style'), '#empty_option' => t('None (original image)'), '#options' => $image_styles, '#description' => array( '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')), '#access' => $this->currentUser->hasPermission('administer image styles'), ), ); $element['image_link_class'] = array( '#title' => t('Class(es) to add to the link'), '#type' => 'textfield', '#default_value' => $this->getSetting('image_link_class'), '#description' => t('Separate multiple classes by spaces.'), ); $element['image_link_rel'] = array( '#title' => t('Rel(s) attribute value to add to the link'), '#type' => 'textfield', '#default_value' => $this->getSetting('image_link_rel'), '#description' => t('Separate multiple rels by spaces.'), ); $element['image_link_image_class'] = array( '#title' => t('Class(es) to add to the image element'), '#type' => 'textfield', '#default_value' => $this->getSetting('image_link_image_class'), '#description' => t('Separate multiple classes by spaces.'), ); return $element; }
/** * Sets up the unrouted url assembler and the link generator. */ protected function setUpUrlIntegrationServices() { $this->pathProcessor = $this->getMock('Drupal\\Core\\PathProcessor\\OutboundPathProcessorInterface'); $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor); \Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler); $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'), $this->renderer); $this->renderer->method('render')->willReturnCallback(function (&$elements, $is_root_call = FALSE) { // Mock the ability to theme links $link = $this->linkGenerator->generate($elements['#title'], $elements['#url']); if (isset($elements['#prefix'])) { $link = $elements['#prefix'] . $link; } if (isset($elements['#suffix'])) { $link = $link . $elements['#suffix']; } return Markup::create($link); }); }
/** * Implements form submit handler. */ public function pay(array $form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); $root_element_parents = array_slice($triggering_element['#array_parents'], 0, -3); $root_element = NestedArray::getValue($form, $root_element_parents); $plugin_selector = $this->getPluginSelector($root_element, $form_state); $plugin_selector->submitSelectorForm($root_element['container']['payment_form']['payment_method'], $form_state); $payment = $this->getPayment($root_element, $form_state); $this->getEntityFormDisplay($root_element, $form_state)->extractFormValues($payment, $root_element['container']['payment_form'], $form_state); $payment_method = $plugin_selector->getSelectedPlugin(); $payment->setPaymentMethod($payment_method); $payment->save(); $result = $payment->execute(); if (!$result->isCompleted() && !$this->requestStack->getCurrentRequest()->isXmlHttpRequest()) { $url = $payment->urlInfo('complete'); $url->setOption('attributes', ['target' => '_blank']); $link = $this->linkGenerator->generate($this->t('Complete payment (opens in a new window).'), $url); drupal_set_message($link); } $form_state->setRebuild(); }
/** * Setup the link generator with a frontpage route. */ public function setupLinkGeneratorWithFrontpage() { $this->linkGenerator->expects($this->once())->method('generate')->with($this->anything(), '<front>', array())->will($this->returnCallback(function ($title) { return '<a href="/">' . $title . '</a>'; })); }