Skip to main content

Posts

Showing posts with the label Drupal 8

Main menu dropdown parent item clickable in Drupal 8 using Bootstrap 4

 Edit menu--main.html.twig template file {# /**  * @file  * Bootstrap Barrio's override to display a menu.  *  * Available variables:  * - menu_name: The machine name of the menu.  * - items: A nested list of menu items. Each menu item contains:  *   - attributes: HTML attributes for the menu item.  *   - below: The menu item child items.  *   - title: The menu link title.  *   - url: The menu link url, instance of \Drupal\Core\Url  *   - localized_options: Menu link localized options.  *   - is_expanded: TRUE if the link has visible children within the current  *     menu tree.  *   - is_collapsed: TRUE if the link has children within the current menu tree  *     that are not currently visible.  *   - in_active_trail: TRUE if the link is in the active trail.  */ #} {% import _self as menus...

Theme table with pager - Drupal 8

public function nodeList() {     $header = [       'Nid',       'Title',     ];     $query = \Drupal::database()->select('node', 'n');     $query->fields('n', ['nid']);     $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);     $results = $pager->execute()->fetchAll();     foreach ($results as $key => $result) {       $node = Node::load($result->nid);       $row = [];       $row = [         'nid' => $node->id(),         'title' => $node->getTitle(),       ];       $rows[] = $row;     }     $build['table_data'] = [       '#type' => 'table',       '#header' => $header,       '#rows' => $rows,     ]; ...

Docksal usage with Drupal

DOCKSAL ======= 1. Drupal installation using composer ref: https://blog.docksal.io/creating-drupal-8-project-from-scratch-using-pre-installed-composer-in-docksal-cli-bfe49b0042e2 # Create new directory for the project mkdir my-new-project # Go to my new dir cd my-new-project # Download Drupal 8 using composer repo recommended in Drupal docs fin run-cli composer create-project drupal-composer/drupal-project:8.x-dev . --stability dev --no-interaction # Initialize Docksal project root mkdir .docksal # Configure DOCROOT to match checked out files fin config set DOCROOT=web # Start my project containers fin project start 2. To start existing project on Docksal   fin project start / fin up 3. DB settings (default)    db name: default    username: user    password: user    host: db 4. Install PHPMyAdmin to project   fin addon install pma   # Then access it like http://pma.project-name.docksal   # T...

SMTP configuration for Gmail | Drupal 8 [Updated]

Use the following configuration

Drupal 8 Ajax Throbber CSS

.ajax-progress, .ajax-progress-throbber, .ajax-progress-fullscreen { width: 100%; height: 100%; margin: 0; padding: 0; -webkit-border-radius: 0; border-radius: 0; opacity: 1; background: rgba(255, 255, 255, 0.8); position: fixed; top: 0; left: 0; z-index: 999999; overflow: hidden; text-indent: -99999em; } .ajax-progress-throbber:before, .ajax-progress-fullscreen:before { content: " "; display: block; width: 120px; height: 120px; -webkit-animation: spin 0.8s infinite linear; animation: spin 0.8s infinite linear; border-radius: 120px; border-width: 10px; border-style: solid; border-color: #D6232F transparent #D6232F transparent; overflow: hidden; text-indent: -99999em; margin: auto; position: absolute; top: 0; left: 0; right: 0; bottom: 0; } @-webkit-keyframes spin { to { transform: rotate(360deg); } } @keyframes spin { to { transform: rotate(360deg); } }

Add JOIN tables in views query alter in Drupal 8

How to add join condition in views programmatically in Drupal 8? use $query->queueTable to implement joins. function hook_views_query_alterViewExecutable $views, QueryPluginBase $query) { $query->queueTable('node__field_accounts', "node_field_data", NULL, 'node__field_accounts'); $query->addWhere($where_last_key, 'node__field_accounts.field_accounts_target_id', $account_values['target_id'], 'IN'); } Method definition Sql::queueTable($table, $relationship = NULL, JoinPluginBase $join = NULL, $alias = NULL) To use INNER JOIN $configuration = [ 'type' => 'INNER', 'table' => 'user__field_region', 'field' => 'entity_id', 'left_table' => 'users_field_data', 'left_field' => 'uid', 'operator' => '=', ]; $join = Views::pluginManager('join') ->...

How to inject database connection in Drupal 8

Database connection can be injected in class object using dependency injection. We can also use service method \Drupal::database() This way of using database is suitable for XXX.module file but in class we can use dependency injection. Using dependency injection :- Drupal\Core\Database\Connection class for database connection. use Drupal\Core\Database\Connection; use Symfony\Component\DependencyInjection\ContainerInterface; class CustomClass { /** * The database connection to be used. * * @var \Drupal\Core\Database\Connection */ protected $database; /** * @param \Drupal\Core\Database\Connection $database * The database connection to be used. */ public function __construct(Connection $database) { $this->database = $database; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { $container->get('database') } } $container->get('database') here database is the ...

How to get field settings in Drupal 8

To get settings of an Entity field in Drupal 8 Use this code: $entity_type='node'; $bundle='article'; $field_name='field_summary_image'; $bundle_fields = \Drupal::getContainer()->get('entity_field.manager')->getFieldDefinitions($entity_type, $bundle); $field_definition = $bundle_fields[$field_name]; $file_directory= $field_definition->getSetting('file_directory');

Drupal 8.4 requires jQuery 3, which no longer supports load()

You might get this error Uncaught TypeError: a.indexOf is not a function at r.fn.init.r.fn.load (jquery.min.js?v=3.2.1:4) at Object.attach (scripts.js?v=8.4.0:1) at Object.Drupal.attachBehaviors (drupal.js?v=8.4.0:25) at drupal.init.js?v=8.4.0:16 at HTMLDocument.t (ready.min.js?v=1.0.8:4) Drupal 8.4  is using latest jQuery version 3.2 which does not support load() function. Instead of using load() , you can use .on(load,function(){})  $(window).on('load', function () { });