Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function validateArguments(array $arguments)
 {
     if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) {
         throw new \InvalidArgumentException(String::format("Invalid extension (@value) specified for the image 'convert' operation", array('@value' => $arguments['extension'])));
     }
     return $arguments;
 }
 /**
  * {@inheritdoc}
  */
 public function generateFieldMetadata(FieldItemListInterface $items, $view_mode)
 {
     $entity = $items->getEntity();
     $field_name = $items->getFieldDefinition()->getName();
     // Early-return if user does not have access.
     $access = $this->accessChecker->accessEditEntityField($entity, $field_name);
     if (!$access) {
         return array('access' => FALSE);
     }
     // Early-return if no editor is available.
     $formatter_id = EntityViewDisplay::collectRenderDisplay($entity, $view_mode)->getRenderer($field_name)->getPluginId();
     $editor_id = $this->editorSelector->getEditor($formatter_id, $items);
     if (!isset($editor_id)) {
         return array('access' => FALSE);
     }
     // Gather metadata, allow the editor to add additional metadata of its own.
     $label = $items->getFieldDefinition()->getLabel();
     $editor = $this->editorManager->createInstance($editor_id);
     $metadata = array('label' => String::checkPlain($label), 'access' => TRUE, 'editor' => $editor_id, 'aria' => t('Entity @type @id, field @field', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id(), '@field' => $label)));
     $custom_metadata = $editor->getMetadata($items);
     if (count($custom_metadata)) {
         $metadata['custom'] = $custom_metadata;
     }
     return $metadata;
 }
Ejemplo n.º 3
0
 /**
  * Adds a language.
  *
  * @param $langcode
  *   The language code of the language to add.
  */
 protected function addLanguage($langcode)
 {
     $edit = array('predefined_langcode' => $langcode);
     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
     $this->container->get('language_manager')->reset();
     $this->assertTrue(\Drupal::languageManager()->getLanguage($langcode), String::format('Language %langcode added.', array('%langcode' => $langcode)));
 }
 /**
  * Overrides \Drupal\tour\Plugin\tour\tour\TipPluginInterface::getOutput().
  */
 public function getOutput()
 {
     $image = array('#theme' => 'image', '#uri' => $this->get('url'), '#alt' => $this->get('alt'));
     $output = '<h2 class="tour-tip-label" id="tour-tip-' . $this->get('ariaId') . '-label">' . String::checkPlain($this->get('label')) . '</h2>';
     $output .= '<p class="tour-tip-image" id="tour-tip-' . $this->get('ariaId') . '-contents">' . drupal_render($image) . '</p>';
     return array('#markup' => $output);
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     foreach ($items as $delta => $item) {
         if (!$item->access) {
             // User doesn't have access to the referenced entity.
             continue;
         }
         /** @var $referenced_entity \Drupal\Core\Entity\EntityInterface */
         if ($referenced_entity = $item->entity) {
             $label = $referenced_entity->label();
             // If the link is to be displayed and the entity has a uri, display a
             // link.
             if ($this->getSetting('link') && ($uri = $referenced_entity->urlInfo())) {
                 $elements[$delta] = array('#type' => 'link', '#title' => $label) + $uri->toRenderArray();
                 if (!empty($item->_attributes)) {
                     $elements[$delta]['#options'] += array('attributes' => array());
                     $elements[$delta]['#options']['attributes'] += $item->_attributes;
                     // Unset field item attributes since they have been included in the
                     // formatter output and shouldn't be rendered in the field template.
                     unset($item->_attributes);
                 }
             } else {
                 $elements[$delta] = array('#markup' => String::checkPlain($label));
             }
             $elements[$delta]['#cache']['tags'] = $referenced_entity->getCacheTag();
         }
     }
     return $elements;
 }
 /**
  * Overrides Drupal\Core\Entity\EntityForm::form().
  *
  * @param array $form
  *   A nested array form elements comprising the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping
  *   The entity being edited.
  *
  * @return array
  *   The array containing the complete form.
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     if ($this->operation == 'duplicate') {
         $form['#title'] = $this->t('<em>Duplicate responsive image mapping</em> @label', array('@label' => $this->entity->label()));
         $this->entity = $this->entity->createDuplicate();
     }
     if ($this->operation == 'edit') {
         $form['#title'] = $this->t('<em>Edit responsive image mapping</em> @label', array('@label' => $this->entity->label()));
     }
     /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
     $responsive_image_mapping = $this->entity;
     $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $responsive_image_mapping->label(), '#description' => $this->t("Example: 'Hero image' or 'Author image'."), '#required' => TRUE);
     $form['id'] = array('#type' => 'machine_name', '#default_value' => $responsive_image_mapping->id(), '#machine_name' => array('exists' => '\\Drupal\\responsive_image\\Entity\\ResponsiveImageMapping::load', 'source' => array('label')), '#disabled' => (bool) $responsive_image_mapping->id() && $this->operation != 'duplicate');
     if ((bool) $responsive_image_mapping->id() && $this->operation != 'duplicate') {
         $description = $this->t('Select a breakpoint group from the enabled themes.') . ' ' . $this->t("Warning: if you change the breakpoint group you lose all your selected mappings.");
     } else {
         $description = $this->t('Select a breakpoint group from the enabled themes.');
     }
     $form['breakpointGroup'] = array('#type' => 'select', '#title' => $this->t('Breakpoint group'), '#default_value' => $responsive_image_mapping->getBreakpointGroup() != '' ? $responsive_image_mapping->getBreakpointGroup()->id() : '', '#options' => breakpoint_group_select_options(), '#required' => TRUE, '#description' => $description);
     $image_styles = image_style_options(TRUE);
     $image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
     foreach ($responsive_image_mapping->getMappings() as $breakpoint_id => $mapping) {
         foreach ($mapping as $multiplier => $image_style) {
             $breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_id);
             $label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']';
             $form['mappings'][$breakpoint_id][$multiplier] = array('#type' => 'select', '#title' => String::checkPlain($label), '#options' => $image_styles, '#default_value' => $image_style, '#description' => $this->t('Select an image style for this breakpoint.'));
         }
     }
     $form['#tree'] = TRUE;
     return parent::form($form, $form_state, $responsive_image_mapping);
 }
Ejemplo n.º 7
0
 /**
  * Creates a node, then tests the tokens generated from it.
  */
 function testNodeTokenReplacement()
 {
     $url_options = array('absolute' => TRUE, 'language' => $this->interfaceLanguage);
     // Create a user and a node.
     $account = $this->createUser();
     /* @var $node \Drupal\node\NodeInterface */
     $node = entity_create('node', array('type' => 'article', 'tnid' => 0, 'uid' => $account->id(), 'title' => '<blink>Blinking Text</blink>', 'body' => array(array('value' => $this->randomName(32), 'summary' => $this->randomName(16), 'format' => 'plain_text'))));
     $node->save();
     // Generate and test sanitized tokens.
     $tests = array();
     $tests['[node:nid]'] = $node->id();
     $tests['[node:vid]'] = $node->getRevisionId();
     $tests['[node:type]'] = 'article';
     $tests['[node:type-name]'] = 'Article';
     $tests['[node:title]'] = String::checkPlain($node->getTitle());
     $tests['[node:body]'] = $node->body->processed;
     $tests['[node:summary]'] = $node->body->summary_processed;
     $tests['[node:langcode]'] = String::checkPlain($node->language()->id);
     $tests['[node:url]'] = url('node/' . $node->id(), $url_options);
     $tests['[node:edit-url]'] = url('node/' . $node->id() . '/edit', $url_options);
     $tests['[node:author]'] = String::checkPlain($account->getUsername());
     $tests['[node:author:uid]'] = $node->getOwnerId();
     $tests['[node:author:name]'] = String::checkPlain($account->getUsername());
     $tests['[node:created:since]'] = \Drupal::service('date')->formatInterval(REQUEST_TIME - $node->getCreatedTime(), 2, $this->interfaceLanguage->id);
     $tests['[node:changed:since]'] = \Drupal::service('date')->formatInterval(REQUEST_TIME - $node->getChangedTime(), 2, $this->interfaceLanguage->id);
     // Test to make sure that we generated something for each token.
     $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
     foreach ($tests as $input => $expected) {
         $output = $this->tokenService->replace($input, array('node' => $node), array('langcode' => $this->interfaceLanguage->id));
         $this->assertEqual($output, $expected, format_string('Sanitized node token %token replaced.', array('%token' => $input)));
     }
     // Generate and test unsanitized tokens.
     $tests['[node:title]'] = $node->getTitle();
     $tests['[node:body]'] = $node->body->value;
     $tests['[node:summary]'] = $node->body->summary;
     $tests['[node:langcode]'] = $node->language()->id;
     $tests['[node:author:name]'] = $account->getUsername();
     foreach ($tests as $input => $expected) {
         $output = $this->tokenService->replace($input, array('node' => $node), array('langcode' => $this->interfaceLanguage->id, 'sanitize' => FALSE));
         $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced.', array('%token' => $input)));
     }
     // Repeat for a node without a summary.
     $node = entity_create('node', array('type' => 'article', 'uid' => $account->id(), 'title' => '<blink>Blinking Text</blink>', 'body' => array(array('value' => $this->randomName(32), 'format' => 'plain_text'))));
     $node->save();
     // Generate and test sanitized token - use full body as expected value.
     $tests = array();
     $tests['[node:summary]'] = $node->body->processed;
     // Test to make sure that we generated something for each token.
     $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.');
     foreach ($tests as $input => $expected) {
         $output = $this->tokenService->replace($input, array('node' => $node), array('language' => $this->interfaceLanguage));
         $this->assertEqual($output, $expected, format_string('Sanitized node token %token replaced for node without a summary.', array('%token' => $input)));
     }
     // Generate and test unsanitized tokens.
     $tests['[node:summary]'] = $node->body->value;
     foreach ($tests as $input => $expected) {
         $output = $this->tokenService->replace($input, array('node' => $node), array('language' => $this->interfaceLanguage, 'sanitize' => FALSE));
         $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced for node without a summary.', array('%token' => $input)));
     }
 }
 /**
  * Assert sorting by field and property.
  */
 public function testSort()
 {
     // Add text field to entity, to sort by.
     entity_create('field_storage_config', array('field_name' => 'field_text', 'entity_type' => 'node', 'type' => 'text', 'entity_types' => array('node')))->save();
     entity_create('field_config', array('label' => 'Text Field', 'field_name' => 'field_text', 'entity_type' => 'node', 'bundle' => 'article', 'settings' => array(), 'required' => FALSE))->save();
     // Build a set of test data.
     $node_values = array('published1' => array('type' => 'article', 'status' => 1, 'title' => 'Node published1 (<&>)', 'uid' => 1, 'field_text' => array(array('value' => 1))), 'published2' => array('type' => 'article', 'status' => 1, 'title' => 'Node published2 (<&>)', 'uid' => 1, 'field_text' => array(array('value' => 2))));
     $nodes = array();
     $node_labels = array();
     foreach ($node_values as $key => $values) {
         $node = Node::create($values);
         $node->save();
         $nodes[$key] = $node;
         $node_labels[$key] = String::checkPlain($node->label());
     }
     $selection_options = array('target_type' => 'node', 'handler' => 'default', 'handler_settings' => array('target_bundles' => array(), 'sort' => array('field' => 'field_text.value', 'direction' => 'DESC')));
     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
     // Not only assert the result, but make sure the keys are sorted as
     // expected.
     $result = $handler->getReferenceableEntities();
     $expected_result = array($nodes['published2']->id() => $node_labels['published2'], $nodes['published1']->id() => $node_labels['published1']);
     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.');
     // Assert sort by base field.
     $selection_options['handler_settings']['sort'] = array('field' => 'nid', 'direction' => 'ASC');
     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
     $result = $handler->getReferenceableEntities();
     $expected_result = array($nodes['published1']->id() => $node_labels['published1'], $nodes['published2']->id() => $node_labels['published2']);
     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by property returned expected values.');
 }
Ejemplo n.º 9
0
 /**
  * Tests the menu functionality.
  */
 function testMenus()
 {
     $this->drupalPlaceBlock('system_menu_block:main');
     // Create a view with a page display and a menu link in the Main Menu.
     $view = array();
     $view['label'] = $this->randomMachineName(16);
     $view['id'] = strtolower($this->randomMachineName(16));
     $view['description'] = $this->randomMachineName(16);
     $view['page[create]'] = 1;
     $view['page[title]'] = $this->randomMachineName(16);
     $view['page[path]'] = $this->randomMachineName(16);
     $view['page[link]'] = 1;
     $view['page[link_properties][menu_name]'] = 'main';
     $view['page[link_properties][title]'] = $this->randomMachineName(16);
     $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
     // Make sure there is a link to the view from the front page (where we
     // expect the main menu to display).
     $this->drupalGet('');
     $this->assertResponse(200);
     $this->assertLink($view['page[link_properties][title]']);
     $this->assertLinkByHref(_url($view['page[path]']));
     // Make sure the link is associated with the main menu.
     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
     $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
     /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
     $link = $menu_link_manager->createInstance('views_view:views.' . $view['id'] . '.page_1');
     $url = $link->getUrlObject();
     $this->assertEqual($url->getRouteName(), 'view.' . $view['id'] . '.page_1', String::format('Found a link to %path in the main menu', array('%path' => $view['page[path]'])));
     $metadata = $link->getMetaData();
     $this->assertEqual(array('view_id' => $view['id'], 'display_id' => 'page_1'), $metadata);
 }
Ejemplo n.º 10
0
 /**
  * Gets the list of links used by this field.
  *
  * @return array
  *   The links which are used by the render function.
  */
 protected function getLinks()
 {
     $links = array();
     foreach ($this->options['fields'] as $field) {
         if (empty($this->view->field[$field]->last_render_text)) {
             continue;
         }
         $title = $this->view->field[$field]->last_render_text;
         $path = '';
         $url = NULL;
         if (!empty($this->view->field[$field]->options['alter']['path'])) {
             $path = $this->view->field[$field]->options['alter']['path'];
         } elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof UrlObject) {
             $url = $this->view->field[$field]->options['alter']['url'];
         }
         // Make sure that tokens are replaced for this paths as well.
         $tokens = $this->getRenderTokens(array());
         $path = strip_tags(String::decodeEntities($this->viewsTokenReplace($path, $tokens)));
         $links[$field] = array('url' => $path ? UrlObject::fromUri('internal:/' . $path) : $url, 'title' => $title);
         if (!empty($this->options['destination'])) {
             $links[$field]['query'] = drupal_get_destination();
         }
     }
     return $links;
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function filter(RouteCollection $collection, Request $request)
 {
     // Generates a list of Symfony formats matching the acceptable MIME types.
     // @todo replace by proper content negotiation library.
     $acceptable_mime_types = $request->getAcceptableContentTypes();
     $acceptable_formats = array_filter(array_map(array($request, 'getFormat'), $acceptable_mime_types));
     $primary_format = $this->contentNegotiation->getContentType($request);
     foreach ($collection as $name => $route) {
         // _format could be a |-delimited list of supported formats.
         $supported_formats = array_filter(explode('|', $route->getRequirement('_format')));
         if (empty($supported_formats)) {
             // No format restriction on the route, so it always matches. Move it to
             // the end of the collection by re-adding it.
             $collection->add($name, $route);
         } elseif (in_array($primary_format, $supported_formats)) {
             // Perfect match, which will get a higher priority by leaving the route
             // on top of the list.
         } elseif (in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) {
             // Move it to the end of the list.
             $collection->add($name, $route);
         } else {
             // Remove the route if it does not match at all.
             $collection->remove($name);
         }
     }
     if (count($collection)) {
         return $collection;
     }
     // We do not throw a
     // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
     // because we don't want to return a 404 status code, but rather a 406.
     throw new NotAcceptableHttpException(String::format('No route found for the specified formats @formats.', array('@formats' => implode(' ', $acceptable_mime_types))));
 }
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function preRender(&$element)
 {
     $element_attributes = new \Drupal\Core\Template\Attribute();
     if ($this->getSetting('attributes')) {
         // This regex split the attributes string so that we can pass that
         // later to drupal_attributes().
         preg_match_all('/([^\\s=]+)="([^"]+)"/', $this->getSetting('attributes'), $matches);
         // Put the attribute and the value together.
         foreach ($matches[1] as $key => $attribute) {
             $element_attributes[$attribute] = $matches[2][$key];
         }
     }
     // Add the classes to the attributes array.
     if ($this->getSetting('classes')) {
         if (!isset($element_attributes['class'])) {
             $element_attributes['class'] = array();
         }
         $element_attributes['class'][] = $this->getSetting('classes');
     }
     $element['#prefix'] = '<' . $this->getSetting('element') . $element_attributes . '>';
     if ($this->getSetting('show_label')) {
         $element['#prefix'] .= '<' . $this->getSetting('label_element') . '><span>';
         $element['#prefix'] .= String::checkPlain(\Drupal::translation()->translate($group->label));
         $element['#prefix'] .= '</span></' . $this->getSetting('label_element') . '>';
     }
     $element['#suffix'] = '</' . $this->getSetting('element') . '>';
 }
Ejemplo n.º 13
0
 /**
  * Simulates submission of a form using GET instead of POST.
  *
  * Forms that use the GET method cannot be submitted with
  * WebTestBase::drupalPostForm(), which explicitly uses POST to submit the
  * form. So this method finds the form, verifies that it has input fields and
  * a submit button matching the inputs to this method, and then calls
  * WebTestBase::drupalGet() to simulate the form submission to the 'action'
  * URL of the form (if set, or the current URL if not).
  *
  * See WebTestBase::drupalPostForm() for more detailed documentation of the
  * function parameters.
  *
  * @param string $path
  *   Location of the form to be submitted: either a Drupal path, absolute
  *   path, or NULL to use the current page.
  * @param array $edit
  *   Form field data to submit. Unlike drupalPostForm(), this does not support
  *   file uploads.
  * @param string $submit
  *   Value of the submit button to submit clicking. Unlike drupalPostForm(),
  *   this does not support AJAX.
  * @param string $form_html_id
  *   (optional) HTML ID of the form, to disambiguate.
  */
 protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL)
 {
     if (isset($path)) {
         $this->drupalGet($path);
     }
     if ($this->parse()) {
         // Iterate over forms to find one that matches $edit and $submit.
         $edit_save = $edit;
         $xpath = '//form';
         if (!empty($form_html_id)) {
             $xpath .= "[@id='" . $form_html_id . "']";
         }
         $forms = $this->xpath($xpath);
         foreach ($forms as $form) {
             // Try to set the fields of this form as specified in $edit.
             $edit = $edit_save;
             $post = array();
             $upload = array();
             $submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form);
             if (!$edit && $submit_matches) {
                 // Everything matched, so "submit" the form.
                 $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : NULL;
                 $this->drupalGet($action, array('query' => $post));
                 return;
             }
         }
         // We have not found a form which contained all fields of $edit and
         // the submit button.
         foreach ($edit as $name => $value) {
             $this->fail(String::format('Failed to set field @name to @value', array('@name' => $name, '@value' => $value)));
         }
         $this->assertTrue($submit_matches, format_string('Found the @submit button', array('@submit' => $submit)));
         $this->fail(format_string('Found the requested form fields at @path', array('@path' => $path)));
     }
 }
Ejemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 protected function validateArguments(array $arguments)
 {
     // Assure at least one dimension.
     if (empty($arguments['width']) && empty($arguments['height'])) {
         throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'scale' operation");
     }
     // Calculate one of the dimensions from the other target dimension,
     // ensuring the same aspect ratio as the source dimensions. If one of the
     // target dimensions is missing, that is the one that is calculated. If both
     // are specified then the dimension calculated is the one that would not be
     // calculated to be bigger than its target.
     $aspect = $this->getToolkit()->getHeight() / $this->getToolkit()->getWidth();
     if ($arguments['width'] && !$arguments['height'] || $arguments['width'] && $arguments['height'] && $aspect < $arguments['height'] / $arguments['width']) {
         $arguments['height'] = (int) round($arguments['width'] * $aspect);
     } else {
         $arguments['width'] = (int) round($arguments['height'] / $aspect);
     }
     // Assure integers for all arguments.
     $arguments['width'] = (int) round($arguments['width']);
     $arguments['height'] = (int) round($arguments['height']);
     // Fail when width or height are 0 or negative.
     if ($arguments['width'] <= 0) {
         throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'scale' operation", array('@value' => $arguments['width'])));
     }
     if ($arguments['height'] <= 0) {
         throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'scale' operation", array('@value' => $arguments['height'])));
     }
     return $arguments;
 }
Ejemplo n.º 15
0
/**
 * Perform a single batch operation.
 *
 * Callback for batch_set().
 *
 * @param $MULTIPLE_PARAMS
 *   Additional parameters specific to the batch. These are specified in the
 *   array passed to batch_set().
 * @param $context
 *   The batch context array, passed by reference. This contains the following
 *   properties:
 *   - 'finished': A float number between 0 and 1 informing the processing
 *     engine of the completion level for the operation. 1 (or no value
 *     explicitly set) means the operation is finished: the operation will not
 *     be called again, and execution passes to the next operation or the
 *     callback_batch_finished() implementation. Any other value causes this
 *     operation to be called again; however it should be noted that the value
 *     set here does not persist between executions of this callback: each time
 *     it is set to 1 by default by the batch system.
 *   - 'sandbox': This may be used by operations to persist data between
 *     successive calls to the current operation. Any values set in
 *     $context['sandbox'] will be there the next time this function is called
 *     for the current operation. For example, an operation may wish to store a
 *     pointer in a file or an offset for a large query. The 'sandbox' array key
 *     is not initially set when this callback is first called, which makes it
 *     useful for determining whether it is the first call of the callback or
 *     not:
 *     @code
 *       if (empty($context['sandbox'])) {
 *         // Perform set-up steps here.
 *       }
 *     @endcode
 *     The values in the sandbox are stored and updated in the database between
 *     http requests until the batch finishes processing. This avoids problems
 *     if the user navigates away from the page before the batch finishes.
 *   - 'message': A text message displayed in the progress page.
 *   - 'results': The array of results gathered so far by the batch processing.
 *     This array is highly useful for passing data between operations. After
 *     all operations have finished, this is passed to callback_batch_finished()
 *     where results may be referenced to display information to the end-user,
 *     such as how many total items were processed.
 */
function callback_batch_operation($MULTIPLE_PARAMS, &$context)
{
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
    if (!isset($context['sandbox']['progress'])) {
        $context['sandbox']['progress'] = 0;
        $context['sandbox']['current_node'] = 0;
        $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
    }
    // For this example, we decide that we can safely process
    // 5 nodes at a time without a timeout.
    $limit = 5;
    // With each pass through the callback, retrieve the next group of nids.
    $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
    while ($row = db_fetch_array($result)) {
        // Here we actually perform our processing on the current node.
        $node_storage->resetCache(array($row['nid']));
        $node = $node_storage->load($row['nid']);
        $node->value1 = $options1;
        $node->value2 = $options2;
        node_save($node);
        // Store some result for post-processing in the finished callback.
        $context['results'][] = String::checkPlain($node->title);
        // Update our progress information.
        $context['sandbox']['progress']++;
        $context['sandbox']['current_node'] = $node->nid;
        $context['message'] = t('Now processing %node', array('%node' => $node->title));
    }
    // Inform the batch engine that we are not finished,
    // and provide an estimation of the completion level we reached.
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
        $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
}
 /**
  * {@inheritdoc}
  */
 protected function doCreate(array $values)
 {
     // We have to determine the bundle first.
     $bundle = FALSE;
     if ($this->bundleKey) {
         if (!isset($values[$this->bundleKey])) {
             throw new EntityStorageException(String::format('Missing bundle for entity type @type', array('@type' => $this->entityTypeId)));
         }
         $bundle = $values[$this->bundleKey];
     }
     $entity = new $this->entityClass(array(), $this->entityTypeId, $bundle);
     foreach ($entity as $name => $field) {
         if (isset($values[$name])) {
             $entity->{$name} = $values[$name];
         } elseif (!array_key_exists($name, $values)) {
             $entity->get($name)->applyDefaultValue();
         }
         unset($values[$name]);
     }
     // Set any passed values for non-defined fields also.
     foreach ($values as $name => $value) {
         $entity->{$name} = $value;
     }
     return $entity;
 }
 /**
  * Tests configuration renaming.
  */
 public function testConfigurationRename()
 {
     $content_type = entity_create('node_type', array('type' => Unicode::strtolower($this->randomName(16)), 'name' => $this->randomName()));
     $content_type->save();
     $staged_type = $content_type->type;
     $active = $this->container->get('config.storage');
     $staging = $this->container->get('config.storage.staging');
     $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
     // Emulate a staging operation.
     $this->copyConfig($active, $staging);
     // Change the machine name of the content type.
     $content_type->type = Unicode::strtolower($this->randomName(8));
     $content_type->save();
     $active_type = $content_type->type;
     $renamed_config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
     $this->assertTrue($active->exists($renamed_config_name), 'The content type has the new name in the active store.');
     $this->assertFalse($active->exists($config_name), "The content type's old name does not exist active store.");
     $this->configImporter()->reset();
     $this->assertEqual(0, count($this->configImporter()->getUnprocessedConfiguration('create')), 'There are no configuration items to create.');
     $this->assertEqual(0, count($this->configImporter()->getUnprocessedConfiguration('delete')), 'There are no configuration items to delete.');
     $this->assertEqual(0, count($this->configImporter()->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
     // We expect that changing the machine name of the content type will
     // rename five configuration entities: the node type, the body field
     // instance, two entity form displays, and the entity view display.
     // @see \Drupal\node\Entity\NodeType::postSave()
     $expected = array('node.type.' . $active_type . '::node.type.' . $staged_type, 'entity.form_display.node.' . $active_type . '.default::entity.form_display.node.' . $staged_type . '.default', 'entity.view_display.node.' . $active_type . '.default::entity.view_display.node.' . $staged_type . '.default', 'entity.view_display.node.' . $active_type . '.teaser::entity.view_display.node.' . $staged_type . '.teaser', 'field.instance.node.' . $active_type . '.body::field.instance.node.' . $staged_type . '.body');
     $renames = $this->configImporter()->getUnprocessedConfiguration('rename');
     $this->assertIdentical($expected, $renames);
     $this->drupalGet('admin/config/development/configuration');
     foreach ($expected as $rename) {
         $names = $this->configImporter()->getStorageComparer()->extractRenameNames($rename);
         $this->assertText(String::format('!source_name to !target_name', array('!source_name' => $names['old_name'], '!target_name' => $names['new_name'])));
         // Test that the diff link is present for each renamed item.
         $href = \Drupal::urlGenerator()->getPathFromRoute('config.diff', array('source_name' => $names['old_name'], 'target_name' => $names['new_name']));
         $this->assertLinkByHref($href);
         $hrefs[$rename] = $href;
     }
     // Ensure that the diff works for each renamed item.
     foreach ($hrefs as $rename => $href) {
         $this->drupalGet($href);
         $names = $this->configImporter()->getStorageComparer()->extractRenameNames($rename);
         $config_entity_type = \Drupal::service('config.manager')->getEntityTypeIdByName($names['old_name']);
         $entity_type = \Drupal::entityManager()->getDefinition($config_entity_type);
         $old_id = ConfigEntityStorage::getIDFromConfigName($names['old_name'], $entity_type->getConfigPrefix());
         $new_id = ConfigEntityStorage::getIDFromConfigName($names['new_name'], $entity_type->getConfigPrefix());
         // Because table columns can be on multiple lines, need to assert a regex
         // pattern rather than normal text.
         $id_key = $entity_type->getKey('id');
         $text = "{$id_key}: {$old_id}";
         $this->assertTextPattern('/\\-\\s+' . preg_quote($text, '/') . '/', "'-{$text}' found.");
         $text = "{$id_key}: {$new_id}";
         $this->assertTextPattern('/\\+\\s+' . preg_quote($text, '/') . '/', "'+{$text}' found.");
     }
     // Run the import.
     $this->drupalPostForm('admin/config/development/configuration', array(), t('Import all'));
     $this->assertText(t('There are no configuration changes.'));
     $this->assertFalse(entity_load('node_type', $active_type), 'The content no longer exists with the old name.');
     $content_type = entity_load('node_type', $staged_type);
     $this->assertIdentical($staged_type, $content_type->type);
 }
Ejemplo n.º 18
0
 public function preRender(&$values)
 {
     $uids = array();
     $this->items = array();
     foreach ($values as $result) {
         $uids[] = $this->getValue($result);
     }
     if ($uids) {
         $roles = user_roles();
         $result = $this->database->query('SELECT u.uid, u.rid FROM {users_roles} u WHERE u.uid IN (:uids) AND u.rid IN (:rids)', array(':uids' => $uids, ':rids' => array_keys($roles)));
         foreach ($result as $role) {
             $this->items[$role->uid][$role->rid]['role'] = String::checkPlain($roles[$role->rid]->label());
             $this->items[$role->uid][$role->rid]['rid'] = $role->rid;
         }
         // Sort the roles for each user by role weight.
         $ordered_roles = array_flip(array_keys($roles));
         foreach ($this->items as &$user_roles) {
             // Create an array of rids that the user has in the role weight order.
             $sorted_keys = array_intersect_key($ordered_roles, $user_roles);
             // Merge with the unsorted array of role information which has the
             // effect of sorting it.
             $user_roles = array_merge($sorted_keys, $user_roles);
         }
     }
 }
Ejemplo n.º 19
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL)
 {
     $account = $this->currentUser();
     $this->user = $user;
     // Prepare the list of shortcut sets.
     $options = array_map(function (ShortcutSet $set) {
         return String::checkPlain($set->label());
     }, $this->shortcutSetStorage->loadMultiple());
     $current_set = shortcut_current_displayed_set($this->user);
     // Only administrators can add shortcut sets.
     $add_access = $account->hasPermission('administer shortcuts');
     if ($add_access) {
         $options['new'] = $this->t('New set');
     }
     $account_is_user = $this->user->id() == $account->id();
     if (count($options) > 1) {
         $form['set'] = array('#type' => 'radios', '#title' => $account_is_user ? $this->t('Choose a set of shortcuts to use') : $this->t('Choose a set of shortcuts for this user'), '#options' => $options, '#default_value' => $current_set->id());
         $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#description' => $this->t('The new set is created by copying items from your default shortcut set.'), '#access' => $add_access, '#states' => array('visible' => array(':input[name="set"]' => array('value' => 'new'))));
         $form['id'] = array('#type' => 'machine_name', '#machine_name' => array('exists' => array($this, 'exists'), 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-'), '#maxlength' => 23, '#states' => array('required' => array(':input[name="set"]' => array('value' => 'new'))), '#required' => FALSE);
         if (!$account_is_user) {
             $default_set = $this->shortcutSetStorage->getDefaultSet($this->user);
             $form['new']['#description'] = $this->t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label()));
         }
         $form['actions'] = array('#type' => 'actions');
         $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Change set'));
     } else {
         // There is only 1 option, so output a message in the $form array.
         $form['info'] = array('#markup' => '<p>' . $this->t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '</p>');
     }
     return $form;
 }
 /**
  * Tests fetchFields.
  */
 public function testFetchFields()
 {
     $views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')->disableOriginalConstructor()->getMock();
     $views_data->expects($this->once())->method('get')->will($this->returnValue($this->viewsData()));
     $data_helper = new ViewsDataHelper($views_data);
     $expected = array('field' => array('age', 'created', 'job', 'name', 'status'), 'argument' => array('age', 'created', 'id', 'job'), 'filter' => array('created', 'id', 'job', 'name', 'status'), 'sort' => array('age', 'created', 'id', 'name', 'status'), 'area' => array('age', 'created', 'job'), 'header' => array('age', 'created', 'job'), 'footer' => array('age', 'created', 'job'));
     $handler_types = array('field', 'argument', 'filter', 'sort', 'area');
     foreach ($handler_types as $handler_type) {
         $fields = $data_helper->fetchFields('views_test_data', $handler_type);
         $expected_keys = $expected[$handler_type];
         array_walk($expected_keys, function (&$item) {
             $item = "views_test_data.{$item}";
         });
         $this->assertEquals($expected_keys, array_keys($fields), String::format('Handlers of type @handler_type are not listed as expected.', array('@handler_type' => $handler_type)));
     }
     // Check for subtype filtering, so header and footer.
     foreach (array('header', 'footer') as $sub_type) {
         $fields = $data_helper->fetchFields('views_test_data', 'area', FALSE, $sub_type);
         $expected_keys = $expected[$sub_type];
         array_walk($expected_keys, function (&$item) {
             $item = "views_test_data.{$item}";
         });
         $this->assertEquals($expected_keys, array_keys($fields), String::format('Sub_type @sub_type is not filtered as expected.', array('@sub_type' => $sub_type)));
     }
 }
Ejemplo n.º 21
0
 /**
  * Test that standard paths works with multiple patterns.
  *
  * @dataProvider getMatchPathData
  */
 public function testMatchPath($patterns, $paths)
 {
     foreach ($paths as $path => $expected_result) {
         $actual_result = $this->pathMatcher->matchPath($path, $patterns);
         $this->assertEquals($actual_result, $expected_result, String::format('Tried matching the path <code>@path</code> to the pattern <pre>@patterns</pre> - expected @expected, got @actual.', array('@path' => $path, '@patterns' => $patterns, '@expected' => var_export($expected_result, TRUE), '@actual' => var_export($actual_result, TRUE))));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL)
 {
     /** @var \Drupal\block\BlockInterface[] $entities */
     $build = array();
     foreach ($entities as $entity) {
         $entity_id = $entity->id();
         $plugin = $entity->getPlugin();
         $plugin_id = $plugin->getPluginId();
         $base_id = $plugin->getBaseId();
         $derivative_id = $plugin->getDerivativeId();
         $configuration = $plugin->getConfiguration();
         // Create the render array for the block as a whole.
         // @see template_preprocess_block().
         $build[$entity_id] = array('#theme' => 'block', '#attributes' => array(), '#contextual_links' => array('block' => array('route_parameters' => array('block' => $entity->id()))), '#weight' => $entity->get('weight'), '#configuration' => $configuration, '#plugin_id' => $plugin_id, '#base_plugin_id' => $base_id, '#derivative_plugin_id' => $derivative_id, '#id' => $entity->id(), '#block' => $entity);
         $build[$entity_id]['#configuration']['label'] = String::checkPlain($configuration['label']);
         // Set cache tags; these always need to be set, whether the block is
         // cacheable or not, so that the page cache is correctly informed.
         $build[$entity_id]['#cache']['tags'] = Cache::mergeTags($this->getCacheTag(), $entity->getCacheTag(), $plugin->getCacheTags());
         if ($plugin->isCacheable()) {
             $build[$entity_id]['#pre_render'][] = array($this, 'buildBlock');
             // Generic cache keys, with the block plugin's custom keys appended
             // (usually cache context keys like 'cache_context.user.roles').
             $default_cache_keys = array('entity_view', 'block', $entity->id(), $this->languageManager->getCurrentLanguage()->getId(), 'cache_context.theme');
             $max_age = $plugin->getCacheMaxAge();
             $build[$entity_id]['#cache'] += array('keys' => array_merge($default_cache_keys, $plugin->getCacheKeys()), 'expire' => $max_age === Cache::PERMANENT ? Cache::PERMANENT : REQUEST_TIME + $max_age);
         } else {
             $build[$entity_id] = $this->buildBlock($build[$entity_id]);
         }
         // Don't run in ::buildBlock() to ensure cache keys can be altered. If an
         // alter hook wants to modify the block contents, it can append another
         // #pre_render hook.
         $this->moduleHandler()->alter(array('block_view', "block_view_{$base_id}"), $build[$entity_id], $plugin);
     }
     return $build;
 }
Ejemplo n.º 23
0
 /**
  * Identifies the children of an element array, optionally sorted by weight.
  *
  * The children of a element array are those key/value pairs whose key does
  * not start with a '#'. See drupal_render() for details.
  *
  * @param array $elements
  *   The element array whose children are to be identified. Passed by
  *   reference.
  * @param bool $sort
  *   Boolean to indicate whether the children should be sorted by weight.
  *
  * @return array
  *   The array keys of the element's children.
  */
 public static function children(array &$elements, $sort = FALSE)
 {
     // Do not attempt to sort elements which have already been sorted.
     $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
     // Filter out properties from the element, leaving only children.
     $children = array();
     $sortable = FALSE;
     foreach ($elements as $key => $value) {
         if ($key === '' || $key[0] !== '#') {
             if (is_array($value)) {
                 $children[$key] = $value;
                 if (isset($value['#weight'])) {
                     $sortable = TRUE;
                 }
             } elseif (isset($value)) {
                 trigger_error(String::format('"@key" is an invalid render array key', array('@key' => $key)), E_USER_ERROR);
             }
         }
     }
     // Sort the children if necessary.
     if ($sort && $sortable) {
         uasort($children, 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
         // Put the sorted children back into $elements in the correct order, to
         // preserve sorting if the same element is passed through
         // \Drupal\Core\Render\Element::children() twice.
         foreach ($children as $key => $child) {
             unset($elements[$key]);
             $elements[$key] = $child;
         }
         $elements['#sorted'] = TRUE;
     }
     return array_keys($children);
 }
Ejemplo n.º 24
0
 /**
  * Checks that configuration complies with its schema on config save.
  *
  * @param \Drupal\Core\Config\ConfigCrudEvent $event
  *   The configuration event.
  *
  * @throws \Drupal\Core\Config\Schema\SchemaIncompleteException
  *   Exception thrown when configuration does not match its schema.
  */
 public function onConfigSave(ConfigCrudEvent $event)
 {
     // Only validate configuration if in the default collection. Other
     // collections may have incomplete configuration (for example language
     // overrides only). These are not valid in themselves.
     $saved_config = $event->getConfig();
     if ($saved_config->getStorage()->getCollectionName() != StorageInterface::DEFAULT_COLLECTION) {
         return;
     }
     $name = $saved_config->getName();
     $data = $saved_config->get();
     $checksum = crc32(serialize($data));
     $exceptions = array('config_schema_test.noschema', 'config_schema_test.someschema', 'config_schema_test.schema_data_types', 'config_schema_test.no_schema_data_types', 'config_test.dynamic.system');
     if (!in_array($name, $exceptions) && !isset($this->checked[$name . ':' . $checksum])) {
         $this->checked[$name . ':' . $checksum] = TRUE;
         $errors = $this->checkConfigSchema($this->typedManager, $name, $data);
         if ($errors === FALSE) {
             throw new SchemaIncompleteException(String::format('No schema for @config_name', array('@config_name' => $name)));
         } elseif (is_array($errors)) {
             $text_errors = [];
             foreach ($errors as $key => $error) {
                 $text_errors[] = String::format('@key @error', array('@key' => $key, '@error' => $error));
             }
             throw new SchemaIncompleteException(String::format('Schema errors for @config_name with the following errors: @errors', array('@config_name' => $name, '@errors' => implode(', ', $text_errors))));
         }
     }
 }
Ejemplo n.º 25
0
 /**
  * Tests the integration.
  */
 public function testIntegration()
 {
     // Remove the watchdog entries added by the potential batch process.
     $this->container->get('database')->truncate('watchdog')->execute();
     $entries = array();
     // Setup a watchdog entry without tokens.
     $entries[] = array('message' => $this->randomMachineName(), 'variables' => array(), 'link' => l('Link', 'node/1'));
     // Setup a watchdog entry with one token.
     $entries[] = array('message' => '@token1', 'variables' => array('@token1' => $this->randomMachineName()), 'link' => l('Link', 'node/2'));
     // Setup a watchdog entry with two tokens.
     $entries[] = array('message' => '@token1 !token2', 'variables' => array('@token1' => $this->randomMachineName(), '!token2' => $this->randomMachineName()), 'link' => l('<object>Link</object>', 'node/2', array('html' => TRUE)));
     foreach ($entries as $entry) {
         $entry += array('type' => 'test-views', 'severity' => WATCHDOG_NOTICE);
         watchdog($entry['type'], $entry['message'], $entry['variables'], $entry['severity'], $entry['link']);
     }
     $view = Views::getView('test_dblog');
     $this->executeView($view);
     $view->initStyle();
     foreach ($entries as $index => $entry) {
         $this->assertEqual($view->style_plugin->getField($index, 'message'), String::format($entry['message'], $entry['variables']));
         $this->assertEqual($view->style_plugin->getField($index, 'link'), Xss::filterAdmin($entry['link']));
     }
     // Disable replacing variables and check that the tokens aren't replaced.
     $view->destroy();
     $view->initHandlers();
     $this->executeView($view);
     $view->initStyle();
     $view->field['message']->options['replace_variables'] = FALSE;
     foreach ($entries as $index => $entry) {
         $this->assertEqual($view->style_plugin->getField($index, 'message'), $entry['message']);
     }
 }
Ejemplo n.º 26
0
 /**
  * Test configuration and subsequent form() and build() method calls.
  *
  * This test is attempting to test the existing block plugin api and all
  * functionality that is expected to remain consistent. The arrays that are
  * used for comparison can change, but only to include elements that are
  * contained within BlockBase or the plugin being tested. Likely these
  * comparison arrays should get smaller, not larger, as more form/build
  * elements are moved into a more suitably responsible class.
  *
  * Instantiation of the plugin is the primary element being tested here. The
  * subsequent method calls are just attempting to cause a failure if a
  * dependency outside of the plugin configuration is required.
  */
 public function testBlockInterface()
 {
     $manager = $this->container->get('plugin.manager.block');
     $configuration = array('label' => 'Custom Display Message');
     $expected_configuration = array('id' => 'test_block_instantiation', 'label' => 'Custom Display Message', 'provider' => 'block_test', 'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE, 'cache' => array('max_age' => 0, 'contexts' => array()), 'display_message' => 'no message set');
     // Initial configuration of the block at construction time.
     /** @var $display_block \Drupal\Core\Block\BlockPluginInterface */
     $display_block = $manager->createInstance('test_block_instantiation', $configuration);
     $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block was configured correctly.');
     // Updating an element of the configuration.
     $display_block->setConfigurationValue('display_message', 'My custom display message.');
     $expected_configuration['display_message'] = 'My custom display message.';
     $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.');
     $definition = $display_block->getPluginDefinition();
     $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400);
     $period = array_map(array(\Drupal::service('date.formatter'), 'formatInterval'), array_combine($period, $period));
     $period[0] = '<' . t('no caching') . '>';
     $period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever');
     $contexts = \Drupal::service("cache_contexts")->getLabels();
     unset($contexts['theme']);
     unset($contexts['languages']);
     $expected_form = array('provider' => array('#type' => 'value', '#value' => 'block_test'), 'admin_label' => array('#type' => 'item', '#title' => t('Block description'), '#markup' => String::checkPlain($definition['admin_label'])), 'label' => array('#type' => 'textfield', '#title' => 'Title', '#maxlength' => 255, '#default_value' => 'Custom Display Message', '#required' => TRUE), 'label_display' => array('#type' => 'checkbox', '#title' => 'Display title', '#default_value' => TRUE, '#return_value' => 'visible'), 'cache' => array('#type' => 'details', '#title' => t('Cache settings'), 'max_age' => array('#type' => 'select', '#title' => t('Maximum age'), '#description' => t('The maximum time this block may be cached.'), '#default_value' => 0, '#options' => $period), 'contexts' => array('#type' => 'checkboxes', '#title' => t('Vary by context'), '#description' => t('The contexts this cached block must be varied by. <em>All</em> blocks are varied by language and theme.'), '#default_value' => array(), '#options' => $contexts, '#states' => array('disabled' => array(':input[name="settings[cache][max_age]"]' => array('value' => (string) 0))))), 'display_message' => array('#type' => 'textfield', '#title' => t('Display message'), '#default_value' => 'My custom display message.'));
     $form_state = new FormState();
     // Ensure there are no form elements that do not belong to the plugin.
     $actual_form = $display_block->buildConfigurationForm(array(), $form_state);
     // Remove the visibility sections, as that just tests condition plugins.
     unset($actual_form['visibility'], $actual_form['visibility_tabs']);
     $this->assertIdentical($actual_form, $expected_form, 'Only the expected form elements were present.');
     $expected_build = array('#children' => 'My custom display message.');
     // Ensure the build array is proper.
     $this->assertIdentical($display_block->build(), $expected_build, 'The plugin returned the appropriate build array.');
     // Ensure the machine name suggestion is correct. In truth, this is actually
     // testing BlockBase's implementation, not the interface itself.
     $this->assertIdentical($display_block->getMachineNameSuggestion(), 'displaymessage', 'The plugin returned the expected machine name suggestion.');
 }
Ejemplo n.º 27
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     // Terms without target_id do not exist yet, theme such terms as just their
     // name.
     foreach ($items as $delta => $item) {
         if (!$item->target_id) {
             $elements[$delta] = array('#markup' => String::checkPlain($item->entity->label()));
         } else {
             /** @var $term \Drupal\taxonomy\TermInterface */
             $term = $item->entity;
             $elements[$delta] = array('#type' => 'link', '#title' => $term->getName(), '#url' => $term->urlInfo());
             if (!empty($item->_attributes)) {
                 $options = $elements[$delta]['#url']->getOptions();
                 $options += ['attributes' => []];
                 $options['attributes'] += $item->_attributes;
                 $elements[$delta]['#url']->setOptions($options);
                 // Unset field item attributes since they have been included in the
                 // formatter output and should not be rendered in the field template.
                 unset($item->_attributes);
             }
             $elements[$delta]['#cache']['tags'] = $item->entity->getCacheTags();
         }
     }
     return $elements;
 }
Ejemplo n.º 28
0
 /**
  * Tests the title_query method.
  *
  * @see \Drupal\user\Plugin\views\argument\RolesRid::title_query()
  */
 public function testTitleQuery()
 {
     $role1 = new Role(array('id' => 'test_rid_1', 'label' => 'test rid 1'), 'user_role');
     $role2 = new Role(array('id' => 'test_rid_2', 'label' => 'test <strong>rid 2</strong>'), 'user_role');
     // Creates a stub entity storage;
     $role_storage = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityStorageInterface');
     $role_storage->expects($this->any())->method('loadMultiple')->will($this->returnValueMap(array(array(array(), array()), array(array('test_rid_1'), array('test_rid_1' => $role1)), array(array('test_rid_1', 'test_rid_2'), array('test_rid_1' => $role1, 'test_rid_2' => $role2)))));
     $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
     $entity_type->expects($this->any())->method('getKey')->with('label')->will($this->returnValue('label'));
     $entity_manager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
     $entity_manager->expects($this->any())->method('getDefinition')->with($this->equalTo('user_role'))->will($this->returnValue($entity_type));
     $entity_manager->expects($this->once())->method('getStorage')->with($this->equalTo('user_role'))->will($this->returnValue($role_storage));
     // @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
     //   entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
     //   the entity manager until this is fixed.
     $container = new ContainerBuilder();
     $container->set('entity.manager', $entity_manager);
     \Drupal::setContainer($container);
     $roles_rid_argument = new RolesRid(array(), 'user__roles_rid', array(), $entity_manager);
     $roles_rid_argument->value = array();
     $titles = $roles_rid_argument->title_query();
     $this->assertEquals(array(), $titles);
     $roles_rid_argument->value = array('test_rid_1');
     $titles = $roles_rid_argument->title_query();
     $this->assertEquals(array('test rid 1'), $titles);
     $roles_rid_argument->value = array('test_rid_1', 'test_rid_2');
     $titles = $roles_rid_argument->title_query();
     $this->assertEquals(array('test rid 1', String::checkPlain('test <strong>rid 2</strong>')), $titles);
 }
 public function testFileParsing()
 {
     $filename = drupal_get_path('module', 'locale') . '/tests/locale_test.js';
     // Parse the file to look for source strings.
     _locale_parse_js_file($filename);
     // Get all of the source strings that were found.
     $strings = $this->container->get('locale.storage')->getStrings(array('type' => 'javascript', 'name' => $filename));
     $source_strings = array();
     foreach ($strings as $string) {
         $source_strings[$string->source] = $string->context;
     }
     $etx = LOCALE_PLURAL_DELIMITER;
     // List of all strings that should be in the file.
     $test_strings = array('Standard Call t' => '', 'Whitespace Call t' => '', 'Single Quote t' => '', "Single Quote \\'Escaped\\' t" => '', 'Single Quote Concat strings t' => '', 'Double Quote t' => '', "Double Quote \\\"Escaped\\\" t" => '', 'Double Quote Concat strings t' => '', 'Context !key Args t' => 'Context string', 'Context Unquoted t' => 'Context string unquoted', 'Context Single Quoted t' => 'Context string single quoted', 'Context Double Quoted t' => 'Context string double quoted', "Standard Call plural{$etx}Standard Call @count plural" => '', "Whitespace Call plural{$etx}Whitespace Call @count plural" => '', "Single Quote plural{$etx}Single Quote @count plural" => '', "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '', "Double Quote plural{$etx}Double Quote @count plural" => '', "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '', "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string', "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted', "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted', "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted');
     // Assert that all strings were found properly.
     foreach ($test_strings as $str => $context) {
         $args = array('%source' => $str, '%context' => $context);
         // Make sure that the string was found in the file.
         $this->assertTrue(isset($source_strings[$str]), String::format('Found source string: %source', $args));
         // Make sure that the proper context was matched.
         $message = $context ? String::format('Context for %source is %context', $args) : String::format('Context for %source is blank', $args);
         $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
     }
     $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
 }
Ejemplo n.º 30
0
 /**
  * {@inheritdoc}
  */
 public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL)
 {
     // @todo Remove when https://www.drupal.org/node/2453059 lands.
     $default_cache_contexts = ['languages', 'theme'];
     /** @var \Drupal\block\BlockInterface[] $entities */
     $build = array();
     foreach ($entities as $entity) {
         $entity_id = $entity->id();
         $plugin = $entity->getPlugin();
         $plugin_id = $plugin->getPluginId();
         $base_id = $plugin->getBaseId();
         $derivative_id = $plugin->getDerivativeId();
         $configuration = $plugin->getConfiguration();
         // Create the render array for the block as a whole.
         // @see template_preprocess_block().
         $build[$entity_id] = array('#theme' => 'block', '#attributes' => array(), '#contextual_links' => array('block' => array('route_parameters' => array('block' => $entity->id()))), '#weight' => $entity->getWeight(), '#configuration' => $configuration, '#plugin_id' => $plugin_id, '#base_plugin_id' => $base_id, '#derivative_plugin_id' => $derivative_id, '#id' => $entity->id(), '#cache' => ['contexts' => Cache::mergeContexts($default_cache_contexts, $plugin->getCacheContexts()), 'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags(), $plugin->getCacheTags()), 'max-age' => $plugin->getCacheMaxAge()], '#block' => $entity);
         $build[$entity_id]['#configuration']['label'] = String::checkPlain($configuration['label']);
         if ($plugin->isCacheable()) {
             $build[$entity_id]['#pre_render'][] = array($this, 'buildBlock');
             // Generic cache keys, with the block plugin's custom keys appended.
             $default_cache_keys = array('entity_view', 'block', $entity->id());
             $build[$entity_id]['#cache']['keys'] = array_merge($default_cache_keys, $plugin->getCacheKeys());
         } else {
             $build[$entity_id] = $this->buildBlock($build[$entity_id]);
         }
         // Don't run in ::buildBlock() to ensure cache keys can be altered. If an
         // alter hook wants to modify the block contents, it can append another
         // #pre_render hook.
         $this->moduleHandler()->alter(array('block_view', "block_view_{$base_id}"), $build[$entity_id], $plugin);
     }
     return $build;
 }