Skip to main content

Posts

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 call non-static method from static method of same class?

You must create a new object inside the static method to access not-static methods inside that class: class Foo { public function fun1 () { echo 'non-static' ; } public static function fun2 () { echo ( new self )-> fun1 (); } } Reference:  https://stackoverflow.com/questions/41631623/how-to-call-non-static-method-from-static-method-of-same-class

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');

How to run jQuery code before Drupal ajax submit call

To run some jQuery code before the Ajax button submit use beforeSubmit function Drupal . behaviors . exampleModule = { attach : function ( context , settings ) { // ELEMENT_ID must not include the # Drupal . ajax [ 'ELEMENT_ID' ]. options . beforeSubmit = function ( form_values , element_settings , options ) { // code here }; } };

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 () { });