/** * Override theme_status_messages() **/ function foundation_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); foreach (drupal_get_messages($display) as $type => $messages) { //convert to foundation classes switch ($type) { case 'error': $type = 'alert'; break; case 'status': $type = 'success'; break; case 'warning': $type = 'secondary'; break; } $output .= "<div data-alert class=\"alert-box {$type}\">\n"; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "<a href=\"\" class=\"close\">×</a></div>\n"; } return $output; }
function aut_status_messages($display = NULL) { $output = ''; foreach (drupal_get_messages($display) as $type => $messages) { if ($type == "error") { $alert = 'alert alert-error'; } elseif ($type == "status") { $alert = 'alert alert-success'; } else { $alert = 'alert'; } $output .= "<div class=\"messages {$type} " . $alert . " \">\n"; if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Returns HTML for status and/or error messages, grouped by type. * * An invisible heading identifies the messages for assistive technology. * Sighted users see a colored box. See http://www.w3.org/TR/WCAG-TECHS/H69.html * for info. * * @param array $variables * An associative array containing: * - display: (optional) Set to 'status' or 'error' to display only messages * of that type. * * @return string * The constructed HTML. * * @see theme_status_messages() * * @ingroup theme_functions */ function bootstrap_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message'), 'info' => t('Informative message')); // Map Drupal message types to their corresponding Bootstrap classes. // @see http://twitter.github.com/bootstrap/components.html#alerts $status_class = array('status' => 'success', 'error' => 'danger', 'warning' => 'warning', 'info' => 'info'); // Retrieve messages. $message_list = drupal_get_messages($display); // Allow the disabled_messages module to filter the messages, if enabled. if (module_exists('disable_messages') && variable_get('disable_messages_enable', '1')) { $message_list = disable_messages_apply_filters($message_list); } foreach ($message_list as $type => $messages) { $class = isset($status_class[$type]) ? ' alert-' . $status_class[$type] : ''; $output .= "<div class=\"alert alert-block{$class} messages {$type}\">\n"; $output .= " <a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a>\n"; if (!empty($status_heading[$type])) { $output .= '<h4 class="element-invisible">' . _bootstrap_filter_xss($status_heading[$type]) . "</h4>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . _bootstrap_filter_xss($message) . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= _bootstrap_filter_xss($messages[0]); } $output .= "</div>\n"; } return $output; }
/** * Overrides theme_status_messages(). */ function bootstrap_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message'), 'info' => t('Informative message')); // Map Drupal message types to their corresponding Bootstrap classes. // @see http://twitter.github.com/bootstrap/components.html#alerts $status_class = array('status' => 'success', 'error' => 'danger', 'warning' => 'warning', 'info' => 'info'); foreach (drupal_get_messages($display) as $type => $messages) { $class = isset($status_class[$type]) ? ' alert-' . $status_class[$type] : ''; $output .= "<div class=\"alert alert-block{$class} messages {$type}\">\n"; $output .= " <a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a>\n"; if (!empty($status_heading[$type])) { $output .= '<h4 class="element-invisible">' . $status_heading[$type] . "</h4>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Implements da_vinci_status_messages(). */ function da_vinci_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div class=\"messages {$type}\">\n"; $output .= "<div class=\"container\">\n"; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= '<span>' . $messages[0] . '</span>'; } $output .= "</div>\n"; $output .= "</div>\n"; } return $output; }
function blackberry_2016_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); foreach (drupal_get_messages($display) as $type => $messages) { // skip all error/warning messages for anonymous users if (!user_is_logged_in() && $type == 'PHP') { } else { $output .= '<div class="alert alert-block alert-' . $type . '"><div class="container"><div class="row"><div class="col-xs-12">'; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . '</h2>'; } if (count($messages) > 1) { $output .= '<ul>'; foreach ($messages as $message) { $output .= ' <li>' . $message . '</li>'; } $output .= '</ul>'; } else { $output .= $messages[0]; } $output .= '</div></div></div><a class="close" data-dismiss="alert" href="#">×</a></div>'; } } return $output; }
function magnetto_status_messages(&$variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); foreach (drupal_get_messages($display) as $type => $messages) { $m_class = $type; if ($type == 'status') { $m_class = 'success'; } $output .= "<div class=\"messages general_info_box {$m_class}\">\n"; if (!empty($status_heading[$type])) { $output .= '<a class="close" href="#">Close</a><h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $field_collection_item = $this->getEntity(); if ($field_collection_item->isNew()) { $host = $this->entityTypeManager->getStorage($this->getRequest()->get('host_type'))->load($this->getRequest()->get('host_id')); $field_collection_item->setHostEntity($host); $field_collection_item->save(); $host->save(); $messages = drupal_get_messages(NULL, false); if (!isset($messages['warning']) && !isset($messages['error'])) { drupal_set_message(t('Successfully added a @type.', array('@type' => $field_collection_item->bundle()))); } } else { $messages = drupal_get_messages(NULL, false); if (!isset($messages['warning']) && !isset($messages['error'])) { $field_collection_item->save(); drupal_set_message(t('Successfully edited %label.', array('%label' => $field_collection_item->label()))); } } if ($field_collection_item->id()) { $form_state->setValue('id', $field_collection_item->id()); $form_state->set('id', $field_collection_item->id()); } else { // In the unlikely case something went wrong on save, the block will be // rebuilt and block form redisplayed. drupal_set_message(t('The field collection item could not be saved.'), 'error'); $form_state->setRebuild(); } /* $form_state->setRedirect( 'field_collection_item.view', array('field_collection_item' => $field_collection_item->id() )); */ }
function hook_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message'), 'info' => 'Status message'); $foundation_css = array('error' => 'warning', 'status' => 'info', 'warning' => 'warning'); foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div data-alert class=\"alert-box {$foundation_css[$type]}\">\n"; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= '<a href="#" class="close">×</a>'; $output .= "</div>\n"; } return $output; }
/** * The basic functionality of drupal_set_message(). */ public function testDrupalSetMessage() { drupal_set_message(t('A message: @foo', ['@foo' => 'bar'])); $messages = drupal_get_messages(); $this->assertInstanceOf('Drupal\\Core\\Render\\Markup', $messages['status'][0]); $this->assertEquals('A message: bar', (string) $messages['status'][0]); }
/** * Create a file and munge/unmunge the name. */ function testMunging() { // Disable insecure uploads. \Drupal::config('system.file')->set('allow_insecure_uploads', 0)->save(); $munged_name = file_munge_filename($this->name, '', TRUE); $messages = drupal_get_messages(); $this->assertTrue(in_array(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $munged_name)), $messages['status']), 'Alert properly set when a file is renamed.'); $this->assertNotEqual($munged_name, $this->name, format_string('The new filename (%munged) has been modified from the original (%original)', array('%munged' => $munged_name, '%original' => $this->name))); }
public function testLegacyDrupal7() { /** @var \Drupal\service_container\Legacy\Drupal7 $drupal7_service */ $drupal7_service = $this->container->get('drupal7'); $this->assertTrue($drupal7_service instanceof Drupal7); $random_message = $this->randomString(); $drupal7_service->drupal_set_message($random_message, 'warning'); $messages = drupal_get_messages(); $this->assertTrue(in_array($random_message, $messages['warning'])); }
/** * Tests Rules default components. */ public function testDefaultComponents() { $config_entity = $this->storage->load('rules_test_default_component'); $user = $this->entityTypeManager->getStorage('user')->create(['mail' => '*****@*****.**']); $config_entity->getComponent()->setContextValue('user', $user)->execute(); // Test that the action was executed correctly. $messages = drupal_get_messages(); $message_string = isset($messages['status'][0]) ? (string) $messages['status'][0] : NULL; $this->assertEquals($message_string, '*****@*****.**'); }
/** * #post_render_cache callback; replaces placeholder with messages. * * Note: this is designed to replace all #post_render_cache placeholders for * messages in a single #post_render_cache callback; hence all placeholders * must be identical. * * @see ::getInfo() * * @param array $element * The renderable array that contains the to be replaced placeholder. * @param array $context * An array with any context information. * * @return array * A renderable array containing the messages. */ public static function renderMessages(array $element, array $context) { $renderer = static::renderer(); // Render the messages. $messages = ['#theme' => 'status_messages', '#message_list' => drupal_get_messages($context['display']), '#status_headings' => ['status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')]]; $markup = $renderer->render($messages); // Replace the placeholder. $callback = get_class() . '::renderMessages'; $placeholder = $renderer->generateCachePlaceholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); $element = $renderer->mergeBubbleableMetadata($element, $messages); return $element; }
/** * Processing the API request. */ public function processRequest(Request $request, RouteMatchInterface $route_match, $service_endpoint_id, $service_definition_id) { /** @var $service_endpoint \Drupal\services\ServiceEndpointInterface */ $service_endpoint = $this->entityManager()->getStorage('service_endpoint')->load($service_endpoint_id); //TODO - pull in settings from service API and alter response /** @var $service_def \Drupal\services\ServiceDefinitionInterface */ $service_def = $this->serviceDefinitionManager->createInstance($service_definition_id, []); /** * Iterate over any contexts defined for this plugin and extract them from * the request defaults if the naming is identical. This means that a * context named 'node' would match to a url parameter {node} or a route * default named 'node'. */ foreach ($service_def->getContextDefinitions() as $context_id => $context_definition) { if ($request->attributes->has($context_id)) { $context = new Context($context_definition, $request->attributes->get($context_id)); $service_def->setContext($context_id, $context); } } // Get the data from the plugin. $data = $service_def->processRequest($request, $route_match, $this->serializer); $code = $service_def->getPluginDefinition()['response_code']; $headers = []; $messages = drupal_get_messages(); if ($messages) { foreach ($messages as $type => $type_message) { $headers["X-Drupal-Services-Messages-{$type}"] = implode("; ", $type_message); } } // Find the request format to determin how we're going to serialize this data $format = $request->getRequestFormat(); $data = $this->serializer->serialize($data, $format); /** * Create a new Cacheable Response object with our serialized data, set its * Content-Type to match the format of our Request and add the service * definition plugin as a cacheable dependency. * * This last step will extract the cache context, tags and max-ages from * any context the plugin required to operate. */ $response = new CacheableResponse($data, $code, $headers); $response->headers->add(['Content-Type' => $request->getMimeType($format)]); $response->addCacheableDependency($service_def); // Be explicit about the caching needs of this response. $response->setVary('Accept'); $service_def->processResponse($response); return $response; }
public function testLegacyMessenger() { /** @var \Drupal\service_container\Messenger\LegacyMessenger $messenger_service */ $messenger_service = $this->container->get('messenger'); $this->assertTrue($messenger_service instanceof LegacyMessenger); $random_message = $this->randomString(); drupal_set_message($random_message, $messenger_service::WARNING); $messages = $messenger_service->getMessages(); $warning_messages = $messages[$messenger_service::WARNING]; $this->assertTrue(in_array($random_message, $warning_messages)); $random_message = $this->randomString(); $messenger_service->addMessage($random_message, $messenger_service::STATUS); $messages = drupal_get_messages(); $status_messages = $messages[$messenger_service::STATUS]; $this->assertTrue(in_array($random_message, $status_messages)); }
/** * Override of theme_status_message(). */ function singular_status_messages($variables) { $display = $variables['display']; $output = ''; $first = TRUE; // Theme specific settings $settings = array( 'style' => theme_get_setting('style'), 'background_color' => theme_get_setting('background_color'), 'background_path' => theme_get_setting('background_path'), 'background_repeat' => theme_get_setting('background_repeat'), 'layout' => theme_get_setting('layout'), 'autoclose' => theme_get_setting('autoclose'), ); $autoclose = isset($settings['autoclose']) ? $settings['autoclose'] : array('status' => 1, 'warning' => 0, 'error' => 0); foreach (drupal_get_messages($display) as $type => $messages) { $class = $first ? 'first' : ''; $class .= !empty($autoclose[$type]) || !isset($autoclose[$type]) ? ' autoclose' : ''; $first = FALSE; $output .= "<div class='messages clear-block $type $class'>"; $output .= "<span class='close'>". t('Hide') ."</span>"; $output .= "<div class='message-content'>"; if (count($messages) > 1) { $output .= "<ul>"; foreach ($messages as $k => $message) { if ($k == 0) { $output .= "<li class='message-item first'>$message</li>"; } else if ($k == count($messages) - 1) { $output .= "<li class='message-item last'>$message</li>"; } else { $output .= "<li class='message-item'>$message</li>"; } } $output .= "</ul>"; } else { $output .= $messages[0]; } $output .= "</div>"; $output .= "</div>"; } return $output; }
function showDrupalMessages($category = 'status', $echo = FALSE) { if (empty($category)) { $s = ''; $msgs = drupal_get_messages(); foreach ($msgs as $type => $msgs1) { $s .= "<br/>{$type} :<br/>"; $s .= implode('<br/>', $msgs1); } } else { $msgs = drupal_get_messages($category); $s = $msgs[$category] ? "<br/>" . implode('<br/>', $msgs[$category]) : ''; } if ($echo) { echo $s; } return $s; }
/** * Theme function for the page status messages. This adds a placeholder * for a close icon. A custom javascript is added to the page for the behavior. */ function ns_theme_status_messages($display = NULL) { $output = ''; foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div class=\"messages {$type}\"><span class=\"icon\"></span>\n"; if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Override the message rendering */ function phptemplate_status_messages($display = NULL) { $output = ''; foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div class=\"messages {$type}\">\n"; $output .= "<h3>{$type}</h3>"; if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Implements the sumbit handler for the ajax call. * * @param array $form * Render array representing from. * @param \Drupal\Core\Form\FormStateInterface $form_state * Current form state. * * @return \Drupal\Core\Ajax\AjaxResponse * Array of ajax commands to execute on submit of the modal form. */ public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) { // At this point the submit handler has fired. // Clear the message set by the submit handler. drupal_get_messages(); // We begin building a new ajax reponse. $response = new AjaxResponse(); if ($form_state->getErrors()) { unset($form['#prefix']); unset($form['#suffix']); $form['status_messages'] = ['#type' => 'status_messages', '#weight' => -10]; $response->addCommand(new HtmlCommand('#fapi-example-modal-form', $form)); } else { $title = $form_state->getValue('title'); $message = t('You specified a title of %title.', ['%title' => $title]); $content = ['#type' => 'html_tag', '#tag' => 'p', '#value' => $message]; $response->addCommand(new HtmlCommand('#fapi-example-message', $content)); $response->addCommand(new CloseModalDialogCommand()); } return $response; }
function hubclip_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); $status_map = array('status' => 'info', 'warning' => 'warning', 'error' => 'alert'); foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div data-alert class=\"alert-box {$status_map[$type]} radius\">\n"; if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "<a href=\"#\" class=\"close\">×</a></div>\n"; } return $output; }
function phptemplate_preprocess_block(&$vars) { $block = $vars['block']; //echo $block->module ." -- ".$block->delta."\n\n"; if ($block->module == 'webform') { $output = ''; foreach (drupal_get_messages(NULL, FALSE) as $type => $messages) { $output .= "<div class=\"messages {$type}\">\n"; if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } $vars['messages'] = $output; } }
/** * status messages */ function subtpl_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); foreach (drupal_get_messages($display) as $type => $messages) { $output .= '<div class="inner padding text-light messages bg-' . $type . '">'; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= reset($messages); } $output .= "</div>\n"; } return $output; }
/** * Implements theme_status_messages(). * * @todo: * -- clean up this mess of concatenation...hard to believe the core contains * this kind of thing. * -- IF we port the main Bootstrap logic to a module, consider providing a * template for status messages. * -- IMPORTANT: alerts sometimes cannot be dismissed by logged-in users; console * doesn't show any errors, but that this affects only authenticated users * suggests that jquery_ui and bootstrap js may be in conflict. */ function newcomer_status_messages(&$variables) { $display = $variables['display']; $output = ''; $message_info = array('status' => array('heading' => 'Status message', 'class' => 'success'), 'error' => array('heading' => 'Error message', 'class' => 'error'), 'warning' => array('heading' => 'Warning message', 'class' => '')); foreach (drupal_get_messages($display) as $type => $messages) { $message_class = $type != 'warning' ? $message_info[$type]['class'] : 'warning'; $output .= "<div class=\"alert alert-block alert-{$message_class} fade in\">\n"; if (!empty($message_info[$type]['heading'])) { $output .= '<h2 class="element-invisible">' . $message_info[$type]['heading'] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Implements theme_status_messages(). */ function omega_bootstrap_status_messages($variables) { $display = $variables['display']; $output = ''; $status_heading = array('status' => t('Status message'), 'error' => t('Error message'), 'warning' => t('Warning message')); $class = array('status' => 'alert alert-success', 'error' => 'alert alert-error', 'warning' => 'alert'); foreach (drupal_get_messages($display) as $type => $messages) { $output .= "<div class=\"{$class[$type]}\">\n"; if (!empty($status_heading[$type])) { $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n"; } if (count($messages) > 1) { $output .= " <ul>\n"; foreach ($messages as $message) { $output .= ' <li>' . $message . "</li>\n"; } $output .= " </ul>\n"; } else { $output .= $messages[0]; } $output .= "</div>\n"; } return $output; }
/** * Override of theme_status_message(). */ function hippelicious_status_messages($display = NULL) { $output = ''; $first = TRUE; // Theme specific settings $settings = theme_get_settings('hippelicious'); $autoclose = isset($settings['autoclose']) ? $settings['autoclose'] : array('status' => 1, 'warning' => 0, 'error' => 0); foreach (drupal_get_messages($display) as $type => $messages) { $class = $first ? 'first' : ''; $class .= !empty($autoclose[$type]) || !isset($autoclose[$type]) ? ' autoclose' : ''; $first = FALSE; $output .= "<div class='messages clear-block {$type} {$class}'>"; $output .= "<span class='close'>" . t('Hide') . "</span>"; $output .= "<div class='message-content'>"; if (count($messages) > 1) { $output .= "<ul>"; foreach ($messages as $k => $message) { if ($k == 0) { $output .= "<li class='message-item first'>{$message}</li>"; } else { if ($k == count($messages) - 1) { $output .= "<li class='message-item last'>{$message}</li>"; } else { $output .= "<li class='message-item'>{$message}</li>"; } } } $output .= "</ul>"; } else { $output .= $messages[0]; } $output .= "</div>"; $output .= "</div>"; } return $output; }
/** * Returns HTML for status and/or error messages, grouped by type. * * Adaptivetheme adds a div wrapper with CSS id. * * An invisible heading identifies the messages for assistive technology. * Sighted users see a colored box. See http://www.w3.org/TR/WCAG-TECHS/H69.html * for info. * * @param $vars * An associative array containing: * - display: (optional) Set to 'status' or 'error' to display only messages * of that type. */ function hwpi_basetheme_status_messages($vars) { $display = $vars['display']; $output = ''; $allowed_html_elements = '<' . implode('><', variable_get('html_title_allowed_elements', array('em', 'sub', 'sup'))) . '>'; $status_heading = array('status' => t('Status update'), 'error' => t('Error'), 'warning' => t('Warning')); foreach (drupal_get_messages($display) as $type => $messages) { $output .= '<div class="messages ' . $type . '"><div class="message-inner"><div class="message-wrapper">'; if (!empty($status_heading[$type])) { $output .= '<h2>' . $status_heading[$type] . "</h2>"; } if (count($messages) > 1) { $output .= " <ul>"; foreach ($messages as $message) { if (strpos($message, 'Biblio') === 0 || strpos($message, 'Publication') === 0) { // Allow some tags in messages about a Biblio. $output .= ' <li>' . strip_tags(html_entity_decode($message), $allowed_html_elements) . "</li>"; } else { $output .= ' <li>' . $message . "</li>"; } } $output .= " </ul>"; } elseif (strpos($messages[0], 'Biblio') === 0 || strpos($messages[0], 'Publication') === 0) { // Allow some tags in messages about a Biblio. $output .= strip_tags(html_entity_decode($messages[0]), $allowed_html_elements); } else { $output .= $messages[0]; } $output .= "</div></div></div>"; } return $output; }
/** * Check several empty values for required forms elements. * * Carriage returns, tabs, spaces, and unchecked checkbox elements are not * valid content for a required field. * * If the form field is found in $form_state->getErrors() then the test pass. */ function testRequiredFields() { // Originates from https://www.drupal.org/node/117748. // Sets of empty strings and arrays. $empty_strings = array('""' => "", '"\\n"' => "\n", '" "' => " ", '"\\t"' => "\t", '" \\n\\t "' => " \n\t ", '"\\n\\n\\n\\n\\n"' => "\n\n\n\n\n"); $empty_arrays = array('array()' => array()); $empty_checkbox = array(NULL); $elements['textfield']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'textfield'); $elements['textfield']['empty_values'] = $empty_strings; $elements['telephone']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'tel'); $elements['telephone']['empty_values'] = $empty_strings; $elements['url']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'url'); $elements['url']['empty_values'] = $empty_strings; $elements['search']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'search'); $elements['search']['empty_values'] = $empty_strings; $elements['password']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'password'); $elements['password']['empty_values'] = $empty_strings; $elements['password_confirm']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'password_confirm'); // Provide empty values for both password fields. foreach ($empty_strings as $key => $value) { $elements['password_confirm']['empty_values'][$key] = array('pass1' => $value, 'pass2' => $value); } $elements['textarea']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'textarea'); $elements['textarea']['empty_values'] = $empty_strings; $elements['radios']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'radios', '#options' => array('' => t('None'), $this->randomMachineName(), $this->randomMachineName(), $this->randomMachineName())); $elements['radios']['empty_values'] = $empty_arrays; $elements['checkbox']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'checkbox', '#required' => TRUE); $elements['checkbox']['empty_values'] = $empty_checkbox; $elements['checkboxes']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'checkboxes', '#options' => array($this->randomMachineName(), $this->randomMachineName(), $this->randomMachineName())); $elements['checkboxes']['empty_values'] = $empty_arrays; $elements['select']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'select', '#options' => array('' => t('None'), $this->randomMachineName(), $this->randomMachineName(), $this->randomMachineName())); $elements['select']['empty_values'] = $empty_strings; $elements['file']['element'] = array('#title' => $this->randomMachineName(), '#type' => 'file'); $elements['file']['empty_values'] = $empty_strings; // Regular expression to find the expected marker on required elements. $required_marker_preg = '@<.*?class=".*?form-required.*?">@'; // Go through all the elements and all the empty values for them. foreach ($elements as $type => $data) { foreach ($data['empty_values'] as $key => $empty) { foreach (array(TRUE, FALSE) as $required) { $form_id = $this->randomMachineName(); $form = array(); $form_state = new FormState(); $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); $element = $data['element']['#title']; $form[$element] = $data['element']; $form[$element]['#required'] = $required; $user_input[$element] = $empty; $user_input['form_id'] = $form_id; $form_state->setUserInput($user_input); $form_state->setFormObject(new StubForm($form_id, $form)); $form_state->setMethod('POST'); // The form token CSRF protection should not interfere with this test, // so we bypass it by setting the token to FALSE. $form['#token'] = FALSE; \Drupal::formBuilder()->prepareForm($form_id, $form, $form_state); \Drupal::formBuilder()->processForm($form_id, $form, $form_state); $errors = $form_state->getErrors(); // Form elements of type 'radios' throw all sorts of PHP notices // when you try to render them like this, so we ignore those for // testing the required marker. // @todo Fix this work-around (https://www.drupal.org/node/588438). $form_output = $type == 'radios' ? '' : \Drupal::service('renderer')->renderRoot($form); if ($required) { // Make sure we have a form error for this element. $this->assertTrue(isset($errors[$element]), "Check empty({$key}) '{$type}' field '{$element}'"); if (!empty($form_output)) { // Make sure the form element is marked as required. $this->assertTrue(preg_match($required_marker_preg, $form_output), "Required '{$type}' field is marked as required"); } } else { if (!empty($form_output)) { // Make sure the form element is *not* marked as required. $this->assertFalse(preg_match($required_marker_preg, $form_output), "Optional '{$type}' field is not marked as required"); } if ($type == 'select') { // Select elements are going to have validation errors with empty // input, since those are illegal choices. Just make sure the // error is not "field is required". $this->assertTrue(empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE, "Optional '{$type}' field '{$element}' is not treated as a required element"); } else { // Make sure there is *no* form error for this element. $this->assertTrue(empty($errors[$element]), "Optional '{$type}' field '{$element}' has no errors with empty input"); } } } } } // Clear the expected form error messages so they don't appear as exceptions. drupal_get_messages(); }
/** * Helper function for the option check test to submit a form while collecting errors. * * @param $form_element * A form element to test. * @param $edit * An array containing post data. * * @return * An array containing the processed form, the form_state and any errors. */ private function formSubmitHelper($form, $edit) { $form_id = $this->randomMachineName(); $form_state = new FormState(); $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); // The form token CSRF protection should not interfere with this test, so we // bypass it by setting the token to FALSE. $form['#token'] = FALSE; $edit['form_id'] = $form_id; // Disable page redirect for forms submitted programmatically. This is a // solution to skip the redirect step (there are no pages, then the redirect // isn't possible). $form_state->disableRedirect(); $form_state->setUserInput($edit); $form_state->setFormObject(new StubForm($form_id, $form)); \Drupal::formBuilder()->prepareForm($form_id, $form, $form_state); \Drupal::formBuilder()->processForm($form_id, $form, $form_state); $errors = $form_state->getErrors(); // Clear errors and messages. drupal_get_messages(); $form_state->clearErrors(); // Return the processed form together with form_state and errors // to allow the caller lowlevel access to the form. return array($form, $form_state, $errors); }