【问题标题】:Adding a CSS class to drupal_set_message将 CSS 类添加到 drupal_set_message
【发布时间】:2018-08-08 21:05:37
【问题描述】:

我正在尝试将 CSS 类添加到 Drupal 中的特定消息,该消息在订阅 mailchimp 列表时成功输出,这是提交功能的代码:

public function submitForm(array &$form, FormStateInterface $form_state) {
global $base_url;

$list_details = mailchimp_get_lists($this->signup->mc_lists);

$subscribe_lists = array();

// Filter out blank fields so we don't erase values on the Mailchimp side.
$mergevars = array_filter($form_state->getValue('mergevars'));

$email = $mergevars['EMAIL'];

$mailchimp_lists = $form_state->getValue('mailchimp_lists');

// If we only have one list we won't have checkbox values to investigate.
if (count(array_filter($this->signup->mc_lists)) == 1) {
  $subscribe_lists[0] = array(
    'subscribe' => reset($this->signup->mc_lists),
    'interest_groups' => isset($mailchimp_lists['interest_groups']) ? $mailchimp_lists['interest_groups'] : NULL,
  );
}
else {
  // We can look at the checkbox values now.
  foreach ($mailchimp_lists as $list) {
    if ($list['subscribe']) {
      $subscribe_lists[] = $list;
    }
  }
}

$successes = array();

// Loop through the selected lists and try to subscribe.
foreach ($subscribe_lists as $list_choices) {
  $list_id = $list_choices['subscribe'];

  $interests = isset($list_choices['interest_groups']) ? $list_choices['interest_groups'] : array();
  if (isset($this->signup->settings['safe_interest_groups']) && $this->signup->settings['safe_interest_groups']) {
    $current_status = mailchimp_get_memberinfo($list_id, $email);
    if (isset($current_status->interests)) {
      $current_interests = array();
      foreach ($current_status->interests as $id => $selected) {
        if ($selected) {
          $current_interests[$id] = $id;
        }
      }
      $interests[] = $current_interests;
    }
  }
  $result = mailchimp_subscribe($list_id, $email, $mergevars, $interests, $this->signup->settings['doublein']);

  if (empty($result)) {
    drupal_set_message(t('There was a problem with your newsletter signup to %list.', array(
      '%list' => $list_details[$list_id]->name,
    )), 'warning');
  }
  else {
    $successes[] = $list_details[$list_id]->name;
  }
}

if (count($successes) && strlen($this->signup->settings['confirmation_message'])) {
  drupal_set_message($this->signup->settings['confirmation_message'], 'status');
}

$destination = $this->signup->settings['destination'];
if (empty($destination)) {
  $destination_url = Url::fromRoute('<current>');
}
else {
  $destination_url = Url::fromUri($base_url . '/' . $this->signup->settings['destination']);
}

$form_state->setRedirectUrl($destination_url);

}

我对修改这部分特别感兴趣:

if (count($successes) && strlen($this->signup->settings['confirmation_message'])) {
  drupal_set_message($this->signup->settings['confirmation_message'], 'status');
}

我想添加一个仅为此确认消息而不是所有消息输出的类。我尝试了几件事:

  1. 根据一些相关的问答,我尝试编辑上面的“状态”部分以在其中添加一个类:“状态配置”或“状态,配置”,这些都不起作用,唯一接受的值是'status'、'warning'和'error',其他值不翻译。

  2. 我也试过这个:

    if (count($successes) && strlen($this->signup->settings['confirmation_message'])) { drupal_set_message('' . $this->signup->settings['confirmation_message'] . '', 'status');

此选项不添加标记,只是将其作为字符串输出:

"<div class="conf">Our confirmation message</div>"

有什么建议吗?

【问题讨论】:

    标签: php string drupal drupal-modules drupal-8


    【解决方案1】:

    twig 模板用于输出消息 html。
    为什么documentation 建议'type' 参数只有3 个选项,我不知道,但这是错误的。状态消息就像任何其他主题(是一个词吗?)输出。

    添加您自己的课程,例如。 drupal_set_message('Our confirmation message', 'conf'); 确实有效,除了类(使用经典主题模板时)将是 messages--conf

    对于“classy”主题,消息模板位于“core/themes/classy/templates/misc/status-messages.html.twig”,如下所示:

    {#
    /**
     * @file
     * Theme override for status messages.
     *
     * Displays status, error, and warning 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.
     *
     * Add an ARIA label to the contentinfo area so that assistive technology
     * user agents will better describe this landmark.
     *
     * Available variables:
     * - message_list: List of messages to be displayed, grouped by type.
     * - status_headings: List of all status types.
     * - attributes: HTML attributes for the element, including:
     *   - class: HTML classes.
     */
    #}
    {% block messages %}
    {% for type, messages in message_list %}
      {%
        set classes = [
          'messages',
          'messages--' ~ type,
        ]
      %}
      <div role="contentinfo" aria-label="{{ status_headings[type] }}"{{ attributes.addClass(classes)|without('role', 'aria-label') }}>
        {% if type == 'error' %}
          <div role="alert">
        {% endif %}
          {% if status_headings[type] %}
            <h2 class="visually-hidden">{{ status_headings[type] }}</h2>
          {% endif %}
          {% if messages|length > 1 %}
            <ul class="messages__list">
              {% for message in messages %}
                <li class="messages__item">{{ message }}</li>
              {% endfor %}
            </ul>
          {% else %}
            {{ messages|first }}
          {% endif %}
        {% if type == 'error' %}
          </div>
        {% endif %}
      </div>
      {# Remove type specific classes. #}
      {% set attributes = attributes.removeClass(classes) %}
    {% endfor %}
    {% endblock messages %}
    

    要覆盖它,只需将您自己的“status-messages.html.twig”添加到您的主题 (MY_THEME/templates/misc/status-messages.html.twig) 并根据需要进行更改。

    【讨论】:

      猜你喜欢
      • 2011-07-16
      • 2023-03-18
      • 2012-08-24
      • 2012-05-19
      • 2020-10-23
      • 2020-11-04
      • 2012-10-02
      • 2020-12-21
      • 2017-06-26
      相关资源
      最近更新 更多