本文整理汇总了PHP中Drupal\Core\Render\Element::children方法的典型用法代码示例。如果您正苦于以下问题:PHP Element::children方法的具体用法?PHP Element::children怎么用?PHP Element::children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Render\Element
的用法示例。
在下文中一共展示了Element::children方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: view
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL)
{
$build = parent::view($entity, $view_mode, $langcode);
if ($view_mode == 'mail') {
// Convert field labels into headings.
// @todo Improve drupal_html_to_text() to convert DIVs correctly.
foreach (Element::children($build) as $key) {
if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') {
$build[$key] += array('#prefix' => '');
$build[$key]['#prefix'] = $build[$key]['#title'] . ":\n";
$build[$key]['#label_display'] = 'hidden';
}
}
$build = array('#markup' => drupal_html_to_text(drupal_render($build)));
}
return $build;
}
示例2: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
foreach (Element::children($form_state->getValue('stock')) as $sku) {
$stock = $form_state->getValue(['stock', $sku]);
db_merge('uc_product_stock')->key(array('sku' => $sku))->updateFields(array('active' => $stock['active'], 'stock' => $stock['stock'], 'threshold' => $stock['threshold']))->insertFields(array('sku' => $sku, 'active' => $stock['active'], 'stock' => $stock['stock'], 'threshold' => $stock['threshold'], 'nid' => $form_state->getValue('nid')))->execute();
}
drupal_set_message($this->t('Stock settings saved.'));
}
示例3: testDrupalRenderSorting
/**
* Tests sorting by weight.
*/
function testDrupalRenderSorting()
{
$first = $this->randomMachineName();
$second = $this->randomMachineName();
// Build an array with '#weight' set for each element.
$elements = array('second' => array('#weight' => 10, '#markup' => $second), 'first' => array('#weight' => 0, '#markup' => $first));
$output = drupal_render($elements);
// The lowest weight element should appear last in $output.
$this->assertTrue(strpos($output, $second) > strpos($output, $first), 'Elements were sorted correctly by weight.');
// Confirm that the $elements array has '#sorted' set to TRUE.
$this->assertTrue($elements['#sorted'], "'#sorted' => TRUE was added to the array");
// Pass $elements through \Drupal\Core\Render\Element::children() and
// ensure it remains sorted in the correct order. drupal_render() will
// return an empty string if used on the same array in the same request.
$children = Element::children($elements);
$this->assertTrue(array_shift($children) == 'first', 'Child found in the correct order.');
$this->assertTrue(array_shift($children) == 'second', 'Child found in the correct order.');
// The same array structure again, but with #sorted set to TRUE.
$elements = array('second' => array('#weight' => 10, '#markup' => $second), 'first' => array('#weight' => 0, '#markup' => $first), '#sorted' => TRUE);
$output = drupal_render($elements);
// The elements should appear in output in the same order as the array.
$this->assertTrue(strpos($output, $second) < strpos($output, $first), 'Elements were not sorted.');
}
示例4: checkoutSettingsForm
/**
* {@inheritdoc}
*/
public function checkoutSettingsForm(array $form, FormStateInterface $form_state, JobInterface $job)
{
if (!Element::children($form)) {
$form['#description'] = t("The @translator translator doesn't provide any checkout settings.", array('@translator' => $job->getTranslator()->label()));
}
return $form;
}
示例5: assertFallbackFormatter
protected function assertFallbackFormatter($entity, array $formatters = array(), array $expected_output)
{
$display = array('type' => 'fallback', 'settings' => array('formatters' => $formatters));
$output = $entity->test_text->view($display);
$output = array_intersect_key($output, Element::children($output));
$this->assertEqual($output, $expected_output);
}
示例6: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$forum_config = $this->config('forum.settings');
$vid = $forum_config->get('vocabulary');
$vocabulary = $this->entityManager->getStorage('taxonomy_vocabulary')->load($vid);
if (!$vocabulary) {
throw new NotFoundHttpException();
}
// Build base taxonomy term overview.
$form = parent::buildForm($form, $form_state, $vocabulary);
foreach (Element::children($form['terms']) as $key) {
if (isset($form['terms'][$key]['#term'])) {
$term = $form['terms'][$key]['#term'];
$form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);
unset($form['terms'][$key]['operations']['#links']['delete']);
$route_parameters = $form['terms'][$key]['operations']['#links']['edit']['url']->getRouteParameters();
if (!empty($term->forum_container->value)) {
$form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit container');
$form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', $route_parameters);
} else {
$form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit forum');
$form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_form', $route_parameters);
}
// We don't want the redirect from the link so we can redirect the
// delete action.
unset($form['terms'][$key]['operations']['#links']['edit']['query']['destination']);
}
}
// Remove the alphabetical reset.
unset($form['actions']['reset_alphabetical']);
// Use the existing taxonomy overview submit handler.
$form['terms']['#empty'] = $this->t('No containers or forums available. <a href="@container">Add container</a> or <a href="@forum">Add forum</a>.', array('@container' => $this->url('forum.add_container'), '@forum' => $this->url('forum.add_forum')));
return $form;
}
示例7: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$filter_values = $this->translateFilterValues();
$langcode = $filter_values['langcode'];
$this->languageManager->reset();
$languages = $this->languageManager->getLanguages();
$langname = isset($langcode) ? $languages[$langcode]->getName() : "- None -";
$form['#attached']['library'][] = 'locale/drupal.locale.admin';
$form['langcode'] = array('#type' => 'value', '#value' => $filter_values['langcode']);
$form['strings'] = array('#type' => 'table', '#tree' => TRUE, '#language' => $langname, '#header' => [$this->t('Source string'), $this->t('Translation for @language', ['@language' => $langname])], '#empty' => $this->t('No strings available.'), '#attributes' => ['class' => ['locale-translate-edit-table']]);
if (isset($langcode)) {
$strings = $this->translateFilterLoadStrings();
$plural_formulas = $this->state->get('locale.translation.plurals') ?: array();
foreach ($strings as $string) {
// Cast into source string, will do for our purposes.
$source = new SourceString($string);
// Split source to work with plural values.
$source_array = $source->getPlurals();
$translation_array = $string->getPlurals();
if (count($source_array) == 1) {
// Add original string value and mark as non-plural.
$plural = FALSE;
$form['strings'][$string->lid]['original'] = array('#type' => 'item', '#title' => $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))), '#title_display' => 'invisible', '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>');
} else {
// Add original string value and mark as plural.
$plural = TRUE;
$original_singular = ['#type' => 'item', '#title' => $this->t('Singular form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>', '#prefix' => '<span class="visually-hidden">' . $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))) . '</span>'];
$original_plural = ['#type' => 'item', '#title' => $this->t('Plural form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[1]) . '</span>'];
$form['strings'][$string->lid]['original'] = [$original_singular, ['#markup' => '<br>'], $original_plural];
}
if (!empty($string->context)) {
$form['strings'][$string->lid]['original'][] = ['#type' => 'inline_template', '#template' => '<br><small>{{ context_title }}: <span lang="en">{{ context }}</span></small>', '#context' => ['context_title' => $this->t('In Context'), 'context' => $string->context]];
}
// Approximate the number of rows to use in the default textarea.
$rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
if (!$plural) {
$form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Translated string (@language)', array('@language' => $langname)), '#title_display' => 'invisible', '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode));
} else {
// Dealing with plural strings.
if (isset($plural_formulas[$langcode]['plurals']) && $plural_formulas[$langcode]['plurals'] > 2) {
// Add a textarea for each plural variant.
for ($i = 0; $i < $plural_formulas[$langcode]['plurals']; $i++) {
$form['strings'][$string->lid]['translations'][$i] = array('#type' => 'textarea', '#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '', '#attributes' => array('lang' => $langcode), '#prefix' => $i == 0 ? '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>' : '');
}
} else {
// Fallback for unknown number of plurals.
$form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Singular form'), '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode), '#prefix' => '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>');
$form['strings'][$string->lid]['translations'][1] = array('#type' => 'textarea', '#title' => $this->t('Plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '', '#attributes' => array('lang' => $langcode));
}
}
}
if (count(Element::children($form['strings']))) {
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save translations'));
}
}
$form['pager']['#type'] = 'pager';
return $form;
}
示例8: setElementErrorsFromFormState
/**
* Stores the errors of each element directly on the element.
*
* We must provide a way for non-form functions to check the errors for a
* specific element. The most common usage of this is a #pre_render callback.
*
* @param array $elements
* An associative array containing the structure of a form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function setElementErrorsFromFormState(array &$elements, FormStateInterface &$form_state)
{
// Recurse through all children.
foreach (Element::children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
$this->setElementErrorsFromFormState($elements[$key], $form_state);
}
}
// Store the errors for this element on the element directly.
$elements['#errors'] = $form_state->getError($elements);
}
示例9: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, CartInterface $cart = NULL)
{
$form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
$cart_config = $this->config('uc_cart.settings');
$form['items'] = array('#type' => 'table', '#tree' => TRUE, '#header' => array('remove' => array('data' => $this->t('Remove'), 'class' => array('remove')), 'image' => array('data' => $this->t('Products'), 'class' => array('image')), 'desc' => array('data' => '', 'class' => array('desc')), 'qty' => array('data' => $this->t('Quantity'), 'class' => array('qty')), 'total' => array('data' => $this->t('Total'), 'class' => array('price'))));
$form['data'] = array('#tree' => TRUE, '#parents' => array('items'));
$i = 0;
$subtotal = 0;
foreach ($cart->getContents() as $cart_item) {
$item = \Drupal::moduleHandler()->invoke($cart_item->data->module, 'uc_cart_display', array($cart_item));
if (Element::children($item)) {
$form['items'][$i]['remove'] = $item['remove'];
$form['items'][$i]['remove']['#name'] = 'remove-' . $i;
$form['items'][$i]['image'] = uc_product_get_picture($item['nid']['#value'], 'uc_cart');
$form['items'][$i]['desc']['title'] = $item['title'];
$form['items'][$i]['desc']['description'] = $item['description'];
$form['items'][$i]['qty'] = $item['qty'];
$form['items'][$i]['total'] = array('#theme' => 'uc_price', '#price' => $item['#total'], '#wrapper_attributes' => array('class' => 'total'));
if (!empty($item['#suffixes'])) {
$form['items'][$i]['total']['#suffixes'] = $item['#suffixes'];
}
$form['data'][$i]['module'] = $item['module'];
$form['data'][$i]['nid'] = $item['nid'];
$form['data'][$i]['data'] = $item['data'];
$form['data'][$i]['title'] = array('#type' => 'value', '#value' => $item['title']['#markup']);
$subtotal += $item['#total'];
}
$i++;
}
$form['items'][]['total'] = array('#theme' => 'uc_price', '#prefix' => '<span id="subtotal-title">' . $this->t('Subtotal') . ':</span> ', '#price' => $subtotal, '#wrapper_attributes' => array('colspan' => 5, 'class' => array('subtotal')));
$form['actions'] = array('#type' => 'actions');
// If the continue shopping element is enabled...
if (($cs_type = $cart_config->get('continue_shopping_type')) !== 'none') {
// Add the element to the form based on the element type.
if ($cart_config->get('continue_shopping_type') == 'link') {
$form['actions']['continue_shopping'] = array('#markup' => $this->l($this->t('Continue shopping'), Url::fromUri('internal:' . $this->continueShoppingUrl())));
} elseif ($cart_config->get('continue_shopping_type') == 'button') {
$form['actions']['continue_shopping'] = array('#type' => 'submit', '#value' => $this->t('Continue shopping'), '#submit' => array(array($this, 'submitForm'), array($this, 'continueShopping')));
}
}
// Add the empty cart button if enabled.
if ($cart_config->get('empty_button')) {
$form['actions']['empty'] = array('#type' => 'submit', '#value' => $this->t('Empty cart'), '#submit' => array(array($this, 'emptyCart')));
}
// Add the control buttons for updating and proceeding to checkout.
$form['actions']['update'] = array('#type' => 'submit', '#name' => 'update-cart', '#value' => $this->t('Update cart'), '#submit' => array(array($this, 'submitForm'), array($this, 'displayUpdateMessage')));
$form['actions']['checkout'] = array('#theme' => 'uc_cart_checkout_buttons');
if ($cart_config->get('checkout_enabled')) {
$form['actions']['checkout']['checkout'] = array('#type' => 'submit', '#value' => $this->t('Checkout'), '#button_type' => 'primary', '#submit' => array(array($this, 'submitForm'), array($this, 'checkout')));
}
$this->renderer->addCacheableDependency($form, $cart);
$this->renderer->addCacheableDependency($form, $cart_config);
return $form;
}
示例10: preRender
/**
* {@inheritdoc}
*/
public function preRender(&$element, $rendering_object)
{
$element += array('#prefix' => '<div class=" ' . implode(' ', $this->getClasses()) . '">', '#suffix' => '</div>', '#tree' => TRUE, '#parents' => array($this->group->group_name), '#default_tab' => '');
if ($this->getSetting('id')) {
$element['#id'] = Html::getId($this->getSetting('id'));
}
// By default tabs don't have titles but you can override it in the theme.
if ($this->getLabel()) {
$element['#title'] = SafeMarkup::checkPlain($this->getLabel());
}
$form_state = new \Drupal\Core\Form\FormState();
if ($this->getSetting('direction') == 'vertical') {
$element += array('#type' => 'vertical_tabs', '#theme_wrappers' => array('vertical_tabs'));
$complete_form = array();
$element = \Drupal\Core\Render\Element\VerticalTabs::processVerticalTabs($element, $form_state, $complete_form);
} else {
$element += array('#type' => 'horizontal_tabs', '#theme_wrappers' => array('horizontal_tabs'));
$on_form = $this->context == 'form';
$element = \Drupal\field_group\Element\HorizontalTabs::processHorizontalTabs($element, $form_state, $on_form);
}
// Make sure the group has 1 child. This is needed to succeed at form_pre_render_vertical_tabs().
// Skipping this would force us to move all child groups to this array, making it an un-nestable.
$element['group']['#groups'][$this->group->group_name] = array(0 => array());
$element['group']['#groups'][$this->group->group_name]['#group_exists'] = TRUE;
// Search for a tab that was marked as open. First one wins.
foreach (\Drupal\Core\Render\Element::children($element) as $tab_name) {
if (!empty($element[$tab_name]['#open'])) {
$element[$this->group->group_name . '__active_tab']['#default_value'] = $tab_name;
break;
}
}
}
示例11: replacePlaceholders
/**
* Will replace placeholders in the #text offsets.
*
* @param array $data
* Data structures where to replace placeholders.
* @param $variables
* Key value pairs.
*/
protected function replacePlaceholders(&$data, $variables)
{
foreach (Element::children($data) as $key) {
if (isset($data[$key]['#text'])) {
$data[$key]['#text'] = (string) new FormattableMarkup($data[$key]['#text'], $variables);
} else {
$this->replacePlaceholders($data[$key], $variables);
}
}
}
示例12: view
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL)
{
// Default the language to the current content language.
if (empty($langcode)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
$elements = $this->viewElements($items, $langcode);
// If there are actual renderable children, use #theme => field, otherwise,
// let access cacheability metadata pass through for correct bubbling.
if (Element::children($elements)) {
$entity = $items->getEntity();
$entity_type = $entity->getEntityTypeId();
$field_name = $this->fieldDefinition->getName();
$info = array('#theme' => 'field', '#title' => $this->fieldDefinition->getLabel(), '#label_display' => $this->label, '#view_mode' => $this->viewMode, '#language' => $items->getLangcode(), '#field_name' => $field_name, '#field_type' => $this->fieldDefinition->getType(), '#field_translatable' => $this->fieldDefinition->isTranslatable(), '#entity_type' => $entity_type, '#bundle' => $entity->bundle(), '#object' => $entity, '#items' => $items, '#formatter' => $this->getPluginId(), '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple());
$elements = array_merge($info, $elements);
}
return $elements;
}
示例13: hook_uc_form_alter
/**
* Allows modules to modify forms before Drupal invokes hook_form_alter().
*
* This hook will normally be used by core modules so any form modifications
* they make can be further modified by contrib modules using a normal
* hook_form_alter(). At this point, drupal_prepare_form() has not been called,
* so none of the automatic form data (e.g.: #parameters, #build_id, etc.) has
* been added yet.
*
* @see hook_form_alter()
*/
function hook_uc_form_alter(&$form, &$form_state, $form_id)
{
// If the node has a product list, add attributes to them
if (isset($form['products']) && count(Element::children($form['products']))) {
foreach (Element::children($form['products']) as $key) {
$form['products'][$key]['attributes'] = _uc_attribute_alter_form(node_load($key));
if (is_array($form['products'][$key]['attributes'])) {
$form['products'][$key]['attributes']['#tree'] = TRUE;
$form['products'][$key]['#type'] = 'details';
}
}
} else {
$form['attributes'] = _uc_attribute_alter_form($node);
if (is_array($form['attributes'])) {
$form['attributes']['#tree'] = TRUE;
$form['attributes']['#weight'] = -1;
}
}
}
示例14: view
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL)
{
$build = parent::view($entity, $view_mode, $langcode);
if ($view_mode == 'mail') {
// Convert field labels into headings.
// @todo Improve \Drupal\Core\Mail\MailFormatHelper::htmlToText() to
// convert DIVs correctly.
foreach (Element::children($build) as $key) {
if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') {
$build[$key] += array('#prefix' => '');
$build[$key]['#prefix'] = $build[$key]['#title'] . ":\n";
$build[$key]['#label_display'] = 'hidden';
}
}
$build['#post_render'][] = function ($html, array $elements) {
return MailFormatHelper::htmlToText($html);
};
}
return $build;
}
示例15: build
/**
* {@inheritdoc}
*/
public function build()
{
$cart = Cart::create(\Drupal::getContainer());
$product_count = count($cart->getContents());
// Display nothing if the block is set to hide on empty and there are no
// items in the cart.
if (!$this->configuration['hide_empty'] || $product_count) {
$items = array();
$item_count = 0;
$total = 0;
if ($product_count) {
foreach ($cart->getContents() as $item) {
$display_item = \Drupal::moduleHandler()->invoke($item->data->module, 'uc_cart_display', array($item));
if (count(Element::children($display_item))) {
$items[] = array('nid' => $display_item['nid']['#value'], 'qty' => $display_item['qty']['#default_value'], 'title' => $display_item['title']['#markup'], 'price' => $display_item['#total'], 'desc' => isset($display_item['description']['#markup']) ? $display_item['description']['#markup'] : FALSE);
$total += $display_item['#total'];
$item_count += $display_item['qty']['#default_value'];
}
}
}
// Build the cart links.
$summary_links['view-cart'] = array('title' => $this->t('View cart'), 'url' => Url::fromRoute('uc_cart.cart'), 'attributes' => array('rel' => ['nofollow']));
// Only add the checkout link if checkout is enabled.
if (\Drupal::config('uc_cart.settings')->get('checkout_enabled')) {
$summary_links['checkout'] = array('title' => $this->t('Checkout'), 'url' => Url::fromRoute('uc_cart.checkout'), 'attributes' => array('rel' => ['nofollow']));
}
$build['block'] = array('#theme' => 'uc_cart_block', '#items' => $items, '#item_count' => $item_count, '#total' => $total, '#summary_links' => $summary_links, '#collapsed' => $this->configuration['collapsed']);
// Add the cart block CSS.
$build['#attached']['library'][] = 'uc_cart/uc_cart_block.styles';
// If the block is collapsible, add the appropriate JS.
if ($this->configuration['collapsible']) {
$build['#attached']['library'][] = 'system/drupal.system';
$build['#attached']['library'][] = 'uc_cart/uc_cart_block.scripts';
}
return $build;
}
}