I
I
Igor2017-03-04 21:35:07
Drupal
Igor, 2017-03-04 21:35:07

How to connect js and new block through own module in Drupal8?

Hello. Please help me figure out how to add js and blocks through my own block. The module itself is activated in the Drupal settings. Here is what is in the LotteryList folder: LotteryList.module
file:

<?php
/**
* @file
* Code for the Chad module.
*/
?>

LotteryList.info.yml file
name: LotteryList Block
description: A simple module to demonstrate Block.
core: 8.x
package: Custom
dependencies:
- node
- block
type: module

LotteryList.libraries.yml file:
global-styling:
version: 1.x
js:
js/custom.js: {}
dependencies:
- core/jquery
- core/jquery.once

Block file in module folder \src\Plugin\Block\LotteryListBlock.php
<?php
/**
* @file
* Contains \Drupal\LotteryList\Plugin\Block\LotteryListBlock.
*/
namespace Drupal\LotteryList\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'LotteryList' block.
*
* @Block(
* id = "lotterylist_block",
* admin_label = @Translation("LotteryList block"),
* category = @Translation("Custom LotteryList block")
* )
*/
class LotteryListBlock extends BlockBase {
/**
* { @inheritdoc}
*/
public function build() {
return array(
'#type' => 'markup',
'
);
}
}

In the js folder, the custom.js file:
(function ($) {
Drupal.behaviors.myModuleBehavior = {
attach: function (context, settings) {
$(context).find('.click-me').once('myCustomBehavior').click(function () {
alert('Hello, World!');
});
}
};
})(jQuery);

Please help me figure it out... Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WiseUkraine, 2017-03-13
@WiseUkraine

1. Drop the folder with the module in site_folder/modules/custom (if "custom" is not present, create it)
2. Enable the module in the admin panel!
3. Profit!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question