2

I want to override the drupal core behavior Drupal.behaviors.filePreviewLinks which is located at core\modules\file\file.js from my custom module

I tried placing the following code in js file of my custom module

Drupal.behaviors.filePreviewLinks = {
    attach: function (context, settings) {
        console.log($(context).find('div.js-form-managed-file .file a'));
        //some custom code
    },
    detach: function (context, settings){
       //some custom code
    }
}; 

But it is not working. What I need to do? How to override Drupal core behavior in custom module?

EDIT

Let me explain my actual question. I am loading a form in page that is created using ECK entity type module. One of a field in form has 'file' as field type. So once I upload a file, it is showing filename with link in a table(I hope it is default drupal feature). I want to remove the file link and show only file name.

  • Not enough info here. Are you attaching this file to the page? – Kevin Apr 6 '18 at 0:18
  • delete Drupal.behaviors.filePreviewLinks.attach; Drupal.behaviors.filePreviewLinks.detach; in your own library loaded afterwards. – user21641 Apr 6 '18 at 12:18
1

I do something similar in the Node Revision Delete module to override the drupal.content_types library doing this:

In the *.libraries.yml file define your library:

content_types:
  version: 1.0.0
  js:
    js/content_types.js: {}
  dependencies:
    - core/jquery
    - core/drupal
    - core/drupal.form

Attack your library where you need it:

  // Element to track the content type.
  $form['workflow']['section']['node_revision_delete_track'] = [
    '#type' => 'checkbox',
    '#title' => t('Limit the amount of revisions for this content type'),
    '#default_value' => $track,
    '#attached' => [
      'library' => [
        'node_revision_delete/content_types',
        'node_revision_delete/admin_settings',
      ],
    ],
  ];

And remove the core library:

/**
 * Implements hook_library_info_alter().
 */
function node_revision_delete_library_info_alter(&$libraries, $extension) {
    unset($libraries['drupal.content_types']);
}
  • This helps to override entire library file. It is working fine and I up-voted for this. Thanks. But, is it possible to override only particular behavior instead of entire library? – siddiq Apr 10 '18 at 3:36
  • @siddiq I don't know (is the first time that I see a problem like this), but you can duplicate the file and rewrite the function that you need to change. – Adrian Cid Almaguer Apr 10 '18 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.