Posts

only show translated menu items into current language (Drupal 8)

function MY_THEME_preprocess_menu(&$variables) {
  if ($variables['menu_name'] == 'brancott-header-menu') {
   $language = Drupal::languageManager()->getCurrentLanguage()->getId();

    foreach ($variables['items'] as $key => &$item) {
      $menuLinkEntity = sapient_brancott_load_link_entity_by_link($item['original_link']);
      if ($menuLinkEntity != NULL) {
        $languages = $menuLinkEntity->getTranslationLanguages();
        // Remove links which is not translated to current language.
        if (!array_key_exists($language, $languages)) {
          unset($variables['items'][$key]);
          continue;
        }
      }

    foreach ($variables['items'][$key]['below'] as $key_child => &$item_child) {
      $menuLinkEntity = sapient_brancott_load_link_entity_by_link($item_child['original_link']);
      if ($menuLinkEntity != NULL) {
        $languages = $menuLinkEntity->getTranslationLa…

Symfony components in Drupal 8.x

Image
The following are the Symfony components that are going to power the Drupal 8 core to a major extent: HttpKernel and HttpFoundation – These are responsible for streamlining the process of converting a Request into a Response with the help of EventDispatcher. Drupal 8 being moved to Symfony was driven by Web Services and Content Core Initiative (WSCCI) in a motive to transform Drupal from a first-class CMS to a first-classREST server with first-class CMS running on top of it. This initiative is intended to allow Drupal to use web services to offer its content with reduced complexity; considering this as a long term vision, Drupal will be more flexible, robust and easily maintainable CMS. EventDispatcher – Implements the Mediator pattern (that uses encapsulation) in a simple and effective manner especially where code inheritance doesn’t work out, making the application highly extensible. It is more effective in situations where you tend to maintain and/or refactor a program consisting of…

Bulk Content Import Drupal 7 (Feeds Module)

Required modules: FeedsSet up CSV: Prepare the Excel file that you will convert to CSV - open up Excel and in the first row add the headers. Feeds importer requires a global ID to uniquely identify nodes. So the first column should contain this unique identifier. Name this column "guid". The other column headers can be anything you like. For example, if you want to import a list of files with title; you can name your columns filename, title, description etc. The column headers do not have to match content type fields. Add data to the Excel file: guid: make sure each row has a unique number.filename: You will have to upload all the files you want to migrate, into your Drupal root directory. So put all your files in a directory called import-uploads and upload them to your Drupal root. Make sure that the filenames have the path from your Drupal root. So each filename should have "import-uploads/" prepended. E.g. "import-uploads/file1.pdf&quo…

How to add extra fields to user profile in Drupal?

Step 1. - Create custom module -For Example My custom module name is extra_user_profile
Folder Name - extra_user_profile
Files - extra_user_profile.nfo, extra_user_profile.module, extra_user_profile.install
Step 2. extra_user_profile.nfo file:- name = Extra User Profile description = Add new field in user profile core = 7.x version = "7.x-2.5" core = "7.x"
Step 3. extra_user_profile.module file:- <?php
Step 4. extra_user_profile.install file:- <?php
/**  * Implementation of hook_enable().  */ function extra_user_profile_enable() {   // Check if our field is not already created.   if (!field_info_field('field_nickname')) {     $field = array(         'field_name' => 'field_nickname',          'type' => 'text',      );     field_create_field($field);
    // Create the instance on the bundle.     $instance = array(         'field_name' => 'field_nickname',          'entity_type' => 'user&#…

PHP explode() Function

Definition and Usage The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.
Example Break a string into an array: <?php $str = "My name is KC Verma"; print_r (explode(" ",$str)); ?>
Result - Array ([0] => My [1]=>name [2]=> is [3]=> KC [4] => Verma) Syntax explode(separator,string,limit)
ParameterDescriptionseparatorRequired. Specifies where to break the stringstringRequired. The string to splitlimitOptional. Specifies the number of array elements to return.Possible values:
Greater than 0 - Returns an array with a maximum of limit element(s)Less than 0 - Returns an array except for the last -limit elements()0 - Returns an array with one element

Entities (Major difference between Drupal 6 and Drupal 7)

Image
Entities Maybe you've heard of "entities" in Drupal 7, wondered what they were, and wanted to learn the underlying concepts. Leveraging the Entity API lets you create more lightweight and flexible solutions. The Drupal community often compares site building through configuration to a favorite childhood toy: LEGO bricks. We can build Entity types, which can make Bundles, to which we can add Fieldsand then create Entities. This article explains the relationships between Entity types > Bundles > Fields > Entities. This was one of the most important changes of Drupal 7, and brought components from some well-loved contributed modules -- such as CCK -- into the core system. The illustration below shows some examples of Entity types included with Drupal 7, with some example entities:
Let’s take a closer look at these concepts. It’s sort of a chicken-and-egg thing, one doesn’t exist without the other. Entity types In earlier versions of Drupal, the field system was only…

What's New in Drupal 7

New Minimum System Requirements: This is not a complete list of requirements. Please read the complete list of requirements.
Database: MySQL 5.0.15 or PostgreSQL 8.3PHP Version 5.2 or higherPHP Memory: 40M - 64M Security:More secure implementation for scheduled tasks (cron.php).More secure password system.More secure log-in system.Modules can be updated via the web. Usability:Administrative links to edit existing page elements are now available on each web page, without having to go to an administration page first.Improved support for integration of WYSIWYG editors.Added more drag-and-drop for administrative tasks.Permissions now have the ability to handle more meta-data (permissions now have a description).User 1 created as part of the installation process.Added features to the default install profile (tagging on the Article content type).Setting up automated task runs (cron) can now be achieved via Drupal's configuration alone, without having to install any scripts on the we…