本文整理汇总了PHP中Drupal\Component\Utility\String类的典型用法代码示例。如果您正苦于以下问题:PHP String类的具体用法?PHP String怎么用?PHP String使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了String类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。
示例1: validateArguments
/**
* {@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;
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:10,代码来源:Convert.php
示例2: generateFieldMetadata
/**
* {@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;
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:28,代码来源:MetadataGenerator.php
示例3: addLanguage
/**
* 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)));
}
开发者ID:anatalsceo,项目名称:en-classe,代码行数:13,代码来源:LocaleUpdateBase.php
示例4: getOutput
/**
* 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);
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:10,代码来源:TipPluginImage.php
示例5: viewElements
/**
* {@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;
}
开发者ID:alnutile,项目名称:drunatra,代码行数:33,代码来源:EntityReferenceLabelFormatter.php
示例6: form
/**
* 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);
}
开发者ID:anatalsceo,项目名称:en-classe,代码行数:44,代码来源:ResponsiveImageMappingForm.php
示例7: testNodeTokenReplacement
/**
* 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)));
}
}
开发者ID:alnutile,项目名称:drunatra,代码行数:63,代码来源:NodeTokenReplaceTest.php
示例8: testSort
/**
* 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.');
}
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:32,代码来源:EntityReferenceSelectionSortTest.php
示例9: testMenus
/**
* 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);
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:34,代码来源:MenuTest.php
示例10: getLinks
/**
* 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;
}
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:31,代码来源:Links.php
示例11: filter
/**
* {@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))));
}
开发者ID:anatalsceo,项目名称:en-classe,代码行数:36,代码来源:AcceptHeaderMatcher.php
示例12: preRender
/**
* {@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') . '>';
}
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:30,代码来源:HtmlElement.php
示例13: submitGetForm
/**
* 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)));
}
}
开发者ID:anatalsceo,项目名称:en-classe,代码行数:60,代码来源:SearchTestBase.php
示例14: validateArguments
/**
* {@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;
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:32,代码来源:Scale.php
示例15: callback_batch_operation
/**
* 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'];
}
}
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:75,代码来源:form.api.php
示例16: doCreate
/**
* {@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;
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:28,代码来源:ContentEntityStorageBase.php
示例17: testConfigurationRename
/**
* 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);
}
开发者ID:alnutile,项目名称:drunatra,代码行数:63,代码来源:NodeTypeRenameConfigImportTest.php
示例18: preRender
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);
}
}
}
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:25,代码来源:Roles.php
示例19: buildForm
/**
* {@inheritdoc}
*/
public function
|
请发表评论