You are here

function beautytips_textinput_form_change in BeautyTips 8

Same name and namespace in other branches
  1. 6.2 includes/textinput.inc \beautytips_textinput_form_change()
  2. 7.2 includes/textinput.inc \beautytips_textinput_form_change()

File

includes/textinput.inc, line 63

Code

function beautytips_textinput_form_change(&$form, $form_state, $form_id) {
  if (\Drupal::state()
    ->get('beautytips_text_input', FALSE)) {
    if (\Drupal::state()
      ->get('beautytips_show_form', FALSE)) {
      $url = Url::fromUserInput('/beautytips/settings/form_ids', [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ]);
      \Drupal::messenger()
        ->addMessage(t("The form_id is %form_id. This message should be @link when finished you're finished checking form_ids.", [
        '%form_id' => $form_id,
        '@link' => Link::fromTextAndUrl(t('turned off'), $url)
          ->toString(),
      ]));
    }
    $add_bt = TRUE;
    if (strlen(\Drupal::state()
      ->get('beautytips_form_id', ''))) {
      if (strpos(\Drupal::state()
        ->get('beautytips_form_id', ''), $form_id) === FALSE) {
        $add_bt = FALSE;
      }
    }
    if ($add_bt) {
      $options = [];
      $options['bt_text_field'] = [
        'cssSelect' => 'input.form-text',
        'trigger' => [
          'focus',
          'blur',
        ],
        'contentSelector' => "\$(this).nextAll('.description:eq(0)').hide().html()",
        'width' => '275px',
        'positions' => [
          0 => \Drupal::state()
            ->get('beautytips_position', 'bottom'),
        ],
        'preEval' => TRUE,
      ];

      // Text areas can have format text with description or just a description.
      // The html structure is different depending on which one is chosen.
      // It would be nice to have a slightly better way of doing this.
      $content = "\n        if (\$(this).parent('.form-textarea-wrapper').nextAll('.description:eq(0)').length !== 0) {\n          \$(this).parent('.form-textarea-wrapper').nextAll('.description:eq(0)').hide().html();\n        }\n        else if (\$(this).parent('.form-textarea-wrapper').parent('.form-item').nextAll('.description:eq(0)').length !== 0) {\n          \$(this).parent('.form-textarea-wrapper').parent('.form-item').nextAll('.description:eq(0)').hide().html();\n        }\n        else {\n          null;\n        }";
      $options['bt_text_area'] = [
        'cssSelect' => 'textarea.form-textarea',
        'trigger' => [
          'focus',
          'dblclick',
        ],
        'contentSelector' => $content,
        'width' => '275px',
        'positions' => [
          0 => \Drupal::state()
            ->get('beautytips_position', 'bottom'),
        ],
        'preEval' => TRUE,
      ];
      beautytips_add_beautytips($options);
    }
  }
}