Drupal: add js function callback to Drupal.settings

Im using a function [drupal_add_js_()][1]to add a fancybox effect to some image in my nodes (they don’t use the Fancybox module becose doesnt fit my needs).

In short, I need to add a function titleFormatto format the image name; In my javascript file, it looks like this:

$("a[rel=myfancy_group]").fancybox({
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'titlePosition': 'over',
    'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
        return '<span><b>' + title + '</b> | Image ' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
    }
});

And here is what my function looks like drupal_add_js:

drupal_add_js(
    array(
        'mycustom_fancybox' => array(
            'selector' => 'div.field-field-immagini-minigallery a',
            'group' => TRUE,
            'options' => array(
                'transitionIn' => 'elastic',
                'transitionOut' => 'elastic',
                'titlePosition' => 'inside',
                'titleShow' => TRUE,
                'width' => 500,
                'cyclic' => TRUE,
                'titleFormat' => ???
            )
        )
    ),
    'setting',
    'footer',
    FALSE,
    TRUE,
    TRUE
);

EDIT I tried:

//add fancybox settings
drupal_add_js(
    array(
        'mycustom_fancybox' => array(
            'selector' => 'div.field-field-immagini-minigallery a',
            'group' => TRUE,
            'options' => array(
                'transitionIn' => 'elastic',
                'transitionOut' => 'elastic',
                'titlePosition' => 'inside',
                'titleShow' => TRUE,
                'width' => 500,
                'cyclic' => TRUE,
                'titleFormat' => "function my_title_format(title, currentArray, currentIndex, currentOpts) { return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>'; }"
            )
        )
    ),
    'setting',
    'footer',
    FALSE,
    TRUE,
    TRUE
);

but (as I expected) Drupal does it like:

"function (title, currentArray, currentIndex, currentOpts) { return \'\x3cspan\x3e\x3cb\x3e\x3ci\x3e\' + title + \'\x3c/i\x3e\x3c/b\x3e | Immagine \' + (currentIndex + 1) + \' di \' + currentArray.length + (title.length ? \' \x26nbsp; \' + title : \'\') + \'\x3c/span\x3e\'; }"

... and it does not work.

I tried

drupal_add_js(
    array(
        'mycustom_fancybox' => array(
            'selector' => 'div.field-field-immagini-minigallery a',
            'group' => TRUE,
            'options' => array(
                'transitionIn' => 'elastic',
                'transitionOut' => 'elastic',
                'titlePosition' => 'inside',
                'titleShow' => TRUE,
                'width' => 500,
                'cyclic' => TRUE,
                'titleFormat' => 'my_title_format'
            )
        )
    ),
    'setting',
    'footer',
    FALSE,
    TRUE,
    TRUE
);
//and, in my js file, added:
function my_title_format(title, currentArray, currentIndex, currentOpts) {
    return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
}

But again doestn work.

+3
source share
2

js Drupal.theme mechanizm. , /block/block.js

    // A custom message for the blocks page specifically.
  Drupal.theme.tableDragChangedWarning = function () {
    return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("The changes to these blocks will not be saved until the <em>Save blocks</em> button is clicked.") + '</div>';
  };

. , js, Drupal.settings, , , .

0

-, ? drupal_add_js - . , ? javascript...

0

Source: https://habr.com/ru/post/1767872/


All Articles