Skip to main content

Posts

Showing posts from 2017

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

Feeds import - Skip importing item based on some condition

You can do this using hook_feeds_presave to check the data prior to it being saved. $item   parameter contains the current feed.  Check item on some condition. To skip an item to being import, use $entity->feeds_item->skip = TRUE; function mymodule_feeds_presave ( FeedsSource $source , $entity , $item ) { // check that this is fired only for the importer being used if ( $source -> importer -> id == 'my_custom_feeds_importer' ){ if ( $item [ 'mfgcode' ] < '18' ){ $entity -> feeds_item -> skip = TRUE ; drupal_set_message ( t ( "Skipping item" , 'warning' ); } } }

Create batch process on form submit in Drupal 7

Using Drupal batch API we can create batch processes. // form submit. function product_display_submit($form, &$form_state) { $products = $form_state['input']['product_table']; if (!empty($products)) { // Remove empty values. $emptyRemoved = remove_empty($products); // re-index array. $myarray = array_values($emptyRemoved); $progress = 0; // where to start $max = count($myarray); // how many records to process until stop. // Tell Drupal about the batch process. $batch = array( 'operations' => array(), 'finished' => 'product_display_batch_finished', // runs after batch is finished 'title' => t('Deleting products'), 'init_message' => t('Beginning product delete'), 'progress_message' => t('Deleted @current out of @total'), 'error_message' => t('Product deletion failed.'), ); //

How to create custom rules condition in Drupal 7

hook_rules_condition_info() is used to create custom rules conditions. /** * Implements hook_rules_condition_info() */ function custom_rules_rules_condition_info() { return array( 'custom_rules_default_condition_dealer_store' => array( 'label' => t('Dealer store'), 'group' => t('Domains'), 'parameter' => array( 'operator' => array( 'type' => 'text', 'label' => t('Show pricing enabled'), 'options list' => '__custom_rules_dealer_store_operators', 'default value' => '0' ) ) ), ); } /** * Custom hook * Test domain for matching * @param string $operator * @return bool */ function custom_rules_default_condition_dealer_store($operator) { $domain_info = domain_get_domain(); // returns 0 or 1. // 1 means current domain is dealer store.

Mapping of XML feeds conditionally using Feed Importer In Drupal 7

Map XML feeds on some basic conditions without using feeds tamper module First, take a look at an example <images>    <image type="file">https://s3.amazon.com/example1.pdf</image>    <image type="file">https://s3.amazon.com/example3.pdf</image>    <image type="file">https://s3.amazon.com/example2.pdf</image>    <image type="image">https://s3.amazon.com/example.jpg</image> </images> Case 1 : If you want to import only those feeds that has type "image" then your XML Parser settings should be like Images/Image[./@type = 'image'] Case 2 : Import feeds that has not file type then Images/Image[./@type != 'file'] Example 2:   < item > < enclosure url = " http://www.example.com/path-to-image/image1.jpg " type = " image/jpeg " /> < enclosure url = " http://www.example.com/path-to-image/image2.jpg

Display payment method name in Drupal Commerce

To display payment method name from payment machine_name programmatically in Drupal Commerce // Display payment method. $r = explode('|', $commerce_order->data['payment_method']); if (count($r) > 1){ $rule = rules_config_load($r[1]); $payment_method = $rule->label; echo "Payment Method: " . $payment_method ; }

Install Atom Text Editor in Ubuntu 16.04

Atom text editor feature: Tweak its UI with CSS and add new features with HTML or Javascript Node.js integration Cross-platform support: Windows, Linux, and OS X. A built-in package manager Smart autocompletion Split Atom interface into multiple panes File system browser Find and replace Support themes How to install Atom in Ubuntu via PPA: Webupd8 Team  is maintaining an unofficial PPA with most recent Atom packages for  all current Ubuntu releases , and derivatives. While official Linux binary is 64-bit only, the PPA supports both 32-bit and 64-bit. 1. Add PPA Open terminal (Ctrl+Alt+T) and run the command: sudo add-apt-repository ppa:webupd8team/atom Type in password when it prompts and hit Enter. 2. Update and install Atom editor: Update system package index and install the text editor via command: sudo apt update; sudo apt install atom Once Atom is installed and a new release is out in future, you can simply upgrade the editor by running regular s

Building Desktop Application with Electron

Electron: Cross-platform Desktop application development library based on node.js and Chromium engine. Electron is an open source library developed by Github for building cross-platform desktop applications with HTML, CSS and Javascript. Electron accomplishes this by combining Chromium and Node.js into a single runtime and apps can be packaged for Mac, Windows, and Linux. Install and Run Electron sample app Note: Git and node.js must be installed on your machine to start with Electron. For Linux, follow these steps: - # Clone the repository $ git clone https://github.com/electron/electron-quick-start # Go into the repository $ cd electron-quick-start # Install dependencies $ npm install # Run the app $ npm start Electron tutorial: https://electron.atom.io/docs/tutorial/quick-start/ https://www.toptal.com/javascript/electron-cross-platform-desktop-apps-easy https://www.tutorialspoint.com/electron/index.htm

Set New Indian Rupee Currency Symbol in Opencart

In 2010 Indian Govt. has launched it's new currency symbol  ₹ . To use this new symbol in your website you can use  &#8377; in your HTML, or if you are using font-awesome library then use  <i class="fa fa-inr"></i> In Opencart you can set currency symbol in localisation setting. If you have tried to copy new rupee symbol from somewhere and paste in Opencart setting. This will not work in some browsers because its not in UTF-8 format. So to use it change the currency.php file in opencart-root/system/library/cart/currency.php (in case of opencart v2.3.x) public function format($number, $currency, $value = '', $format = true) { $symbol_left = $this->currencies[$currency]['symbol_left']; $symbol_right = $this->currencies[$currency]['symbol_right']; $decimal_place = $this->currencies[$currency]['decimal_place']; if (!$value) { $value = $this->currencies[$currency]['value']; } $amount