Drupal 8
more_horiz
close

on 18th December 2017 / by akhila.vn
A patch is a small file which shows what was changed in a repository. It shows the new changes in an existing file, details of the new files in the current directory, file deletion details etc. A patch file can be pushed to the git repository so that it is useful in the future for updating the changes in the corresponding file. For creating a patch file in your current git repository, make changes in your files. Now open the terminal. For unstaged changes, type the following command, git diff > new-changes.patchFor staged changes, you have to use git diff --cached. That is, git diff --cached > new-changes.patchTo apply the patch, use: git apply -v new-changes.patchNote that, if the changes is already there when applying your patch, error message will be shown. In Drupal.org, you can contribute patches to the existing core or contributed modules. For this, First clone the latest version of Drupal (if it is a contributed module clone the latest code from its version control) You have to create the issue with necessary issue summary in Drupal.org based on the fix After making the changes, create a patch file based on the issue number as,git diff > [issue-description]-[issue-number]-[comment-number].patch Update the issue with necessary comment and patch file Make it's status as 'Needs Review' If you want to test the patch file, add it to the queue by clicking the 'Add test' link shown below of the updated patch file We can create and add inter diff for adding new modification for an existing patch file in an issue in Drupal.org, if the changes are not in the latest version of the module. For this, Apply the existing patch to the latest version of module Add your changes to the module Create a new patch from git diff using git diff > [issue-description]-[issue-number]-[comment-number].patch Create an inter diff from the old and new patches as:interdiff old-file.patch new-file.patch > interdiff-[issue_id]-[old_comment_number]-[new_comment_number].txt Upload the newly created patch and the inter diff to the created issue in Drupal.org. An interdiff file tells you what new changes are there with the existing patch and newly created patch. It provides the information of the changes in patch files. So we can make the file as test free using 'do not test' option in the issues itself. So for interdiff to be ignored by the testbot, we can name the interdiff file as: interdiff-[issue_id]-[old_comment_number]-[new_comment_number]-do-not-test.diffHope this information helps in your patch creation. Drupal 7 Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 06th July 2017 / by webmaster
Authors: Adrian McDonald Tariang, Anish M.M. Drupal 8 Render Pipeline is the sequence of steps through which Drupal 8 generates a response to an HTTP request. ie, it’s what’s going on under the hood of Drupal 8. As summer interns, we were trying to understand this process. While going through online material available on the topic, we noted that almost half of it was aimed at people transitioning from Drupal 7 to Drupal 8. Newbies as were to Drupal, we did struggle to figure it out. While digging through the material, it is quite easy to be misled and arrive at wrong conclusions. So, we have come up with this presentation, which targets beginners and keeps it as simple as possible. This presentation is a walk through of the steps followed by Drupal 8 between getting a request and sending back the corresponding response, in that order, with specific details and examples mentioned when deemed necessary. It will provide an overview of the whole process, with emphasis on the Event Mechanism and the Routing Process. The presentation will, we hope, give you a broad understanding of the topic and give you pointers on areas to focus. You are encouraged to follow up on this presentation by going through the references mentioned at the end. We cannot emphasize enough the benefits of going through the code, and urge you to download Drupal 8 code and go through it. A little bit of tweaking in there can go a long way in getting to know the concepts. The online documentation of the code will also come in handy. Few of the tools you could use the blackfire.io or the XHProf profiler, can help in understanding Drupal’s routing system and dependency model debug_backtrace() for local system debugging. The devel module also provides a ddebug_backtrace() that provides a more readable output Learn Drupal 8 Render Pipeline from Zyxware Technologies In Brief Drupal 8 adopts most of its functionality from the Symfony Framework to implement modern PHP standards and is a platform for more developers to enter the Drupal Community. Front Controller The journey begins at the Front Controller, the index.php file, a construct that serves as an entry and exit point for all communications with Drupal. This is where Drupal wraps the request from the user into a Symfony Response Object for a more object oriented approach for request handling. The Request object is then passed through Symfony’s HttpKernel::handle() method, the backbone of the render pipeline that uses an event-based architecture to delegate the task at hand. This system makes Drupal easily extensible, as Developers can now create their own Subscribers to events and modify the end response. Drupal 8 uses the concept of a Controller, a section of code which performs the main application logic. The Controller is assigned through a process called Routing, where, each path( say /admin), is mapped to its corresponding controller. Response The Controller returns its results in the form of a Response object, ready to be sent back to the user, or a classic Drupal Render Array. This Render Array is then pushed to the MainContentViewSubscriber, which decides how the array is to be converted into the Response object. This process, called Rendering, is performed by a Content Renderer, based on the format the response needs to be sent in. Now, when a Response Object is received, Drupal prepares placeholders present on it’s HTML, so as to allow them to be replaced with content by popular placeholder strategies such as ESI, Big Pipe or SingleFlush, after which the final headers and assets are attached to the Response. This Response is then sent back to the user, completing its journey through the Render Pipeline. Credits Wim Leers, author to material such as the render pipeline diagram and the DrupalCon Los Angeles 2015: Drupal 8's render pipeline session. These were extensively referred to for this presentation. References: Site Point: From Request to ResponseDrupal Render API DocumentationDrupal 8 Render Pipeline talkSymfony 2 HttpKernel ComponentDrupal API Documentation Drupal 8 Drupal Planet Symfony Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 05th December 2016 / by deepak.r
Everyone knows Drush is a very important tool in Drupal development, to execute things faster and to manage Drupal websites remotely. Drupal specifically built its shell function to update its configurations, managing modules, running drupal updates, etc. For Drupal 8 older versions of Drush will not work as part of changes that Drupal 8 put forward. If you want to setup the latest version of Drush, you can follow these steps to ensure that latest version of Drush has been installed and which is compatible for Drupal 8. Else, you can update Drush using composer. Open a terminal window and type: cd ~/ Download the latest version of Drush: php -r "readfile('http://files.drush.org/drush.phar');" > drush Make the newly downloaded version executable: sudo chmod +x ~/drush Depending on the setup, You may need to find current install path of Drush by typing: which drush It will return something like '/usr/bin/local', which can be different depending on the package manager in the system Create a backup for current Drush installation: sudo cp /yourpath/to/install/drush /yourpath/to/install/drush_older Delete the older version from your install path ('/usr/bin/local'): sudo rm /yourpath/to/install/drush Move the newly downloaded version to the install path: sudo mv ~/drush /yourpath/to/install/drush Initialize the newest version of Drush: /yourpath/to/install/drush init Navigate to the Drupal 8 root in terminal and simply type 'drush'. If everything is fine, a list of commands will be available for you. By following this, you can ensure that Drush has been updated to the latest version, preserving the older copy of installation, which can be used to switch between Drush versions later. Hope this helps! Drupal Drupal 8 Drush Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 01st December 2016 / by deepak.r
While setting up local environments for Drupal 8, everyone may find difficulty in login to the site, if the password is unknown In Drupal core there is a script file that return the hashed password, which is core/scripts/password-hash.sh. Run the following command to get the hashed string for '123' in terminal. ./core/scripts/password-hash.sh 123Update the returned string in the password field in the users table for required users via command line or any MySQL (or any database systems like PostgreSQL, MariaDB, MongoDB) GUI tools. Hope this helps! Please feel free to get in touch with us if any queries. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 03rd November 2016 / by akhila.vn
In one of my project I want to create user roles programmatically and the role should be enable and disable while module install and uninstall process. So first of all I need to create a YAML file based on the user role name with necessary definitions. The name of the YAML file should be the format user.role.sample.yml. Here sample is my user role id. To make the role enable and disable while module installation and uninstall we have to add dependency with enforced property. Note that the YAML file should be stored in YOURMODULE/config/install folder. The definitions that I have added in the file as: langcode: en status: true dependencies: enforced: module: - mymodule # This is the name of the module. id: sample label: 'Sample Role' weight: 3 is_admin: false permissions: { }After YAML file creation I have uninstalled and install 'mymodule', and found that its working fine. Hope this helps. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 01st November 2016 / by webmaster
On 31st October 2016, Prime Minister of India, Sri Narendra Modi inaugurated the digital exhibition “Uniting India: Sardar Patel” including the web portal, http://sardarpatel.nvli.in/ at Pragati Maidan in New Delhi. The website was developed by Zyxware Technologies in partnership with IIT Bombay as part of the National Virtual Library of India (NVLI) initiative of Ministry of Culture, Government of India under the National Mission of Libraries . The portal showcases the life of Sardar Vallabhai Patel and was unveiled on the occasion of Rashtriya Ekta Diwas, the birth anniversary of Sardar Vallabhai Patel. While speaking on the occasion, Modi stressed on the need of the hour to have that same unity that Sardar Patel worked hard for. Sardar Vallabhai Patel had played an integral role in bringing the princely states under one union. The site is built on Drupal 8 and showcases information about Sardar Patel collected from various libraries along with the ability to translate it to different languages. A timeline on the site along with interesting facts, quotes, and a quiz tells us who the man is and how he came to be known as the ‘Iron Man of India.’ Photo courtesy: http://www.pmindia.gov.in/en/ News Zyxware Drupal 8 Leave a reply Your email address will not be published. Required fields are marker * M S Labba (not verified) access_time 25 May 2019 - 13:05 This is really something to be proud of... Great work guys, Keep going.. Add new comment
more_horiz
close

on 26th October 2016 / by akhila.vn
To load a node details using its id in Drupal 8 use the following syntax: $node_details = Node::load($nid); $node_details->field_FIELD_NAME->value;We can load multiple nodes in Drupal 8 using loadMultiple() function. Note that the parameter should be array of ids. $nodes = Node::loadMultiple($nids); foreach ($nodes as $node_content) { $title = $node_content->getTitle(); $created_by = $node_content->getOwner(); $type = $node_content->getType(); $field1 = $node_content->field_FIELD_NAME->value; // Get necessary fileds like this. }If it is a node page, then we can get $node_content = \Drupal::routeMatch()->getParameter('node');. Similarly we can load file by id and get its contents like this: $file = File::load($file_fid); $file_url = $file->url(); $file_name = $file->getFilename(); $url = Url::fromUri($file_url); $file_user_entity = $file->getOwner() $file_user_id = $file->getOwnerId() $size = $file->getSize(); // Getting all other details of the file goes here. We can load multiple files like node using File::loadMultiple($fids). For taxonomy term loading we have to pass its target id Term::load($keyword_name->target_id);. We can use getName(), getVocabularyId(), getDescription() etc. For vocabularies we can use, Vocabulary::load($vid). If there is an object for vocabulary then we can get the vocabulary hierarchy by using $vb_obj->getHierarchy(). Note that we have to use the entity class for appropriate load use Drupal\node\Entity\ENTITY_NAME;. For example: use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\file\Entity\File; ---------------------------Hope this helps. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 14th October 2016 / by akhila.vn
I want to programmatically create taxonomy vocabulary and it should be enabled and disabled while module install and uninstall process. So first of all I have created a YAML file based on the vocabulary name with necessary definitions. The name of the YAML file should be the format taxonomy.vocabulary.keywords.yml. Here 'keywords' is my 'vid'. To make it proper as specified while module installation and uninstall, I have added the dependency with 'enforced' property. Note that the YAML file should be stored in YOURMODULE/config/install folder otherwise it will not work. The definitions that I have added in the file as follows: langcode: en status: true dependencies: enforced: module: - sample_module name: Keywords vid: keywords description: 'Stores the keywords for sample_module' hierarchy: 0 weight: 0After YAML file creation I have uninstalled and installed 'sample_module', and it worked. Hope this helps in your coding. Please feel free to get in touch with us for any queries. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 09th September 2016 / by deepak.r
Drupal 8's routing system works with the Symfony HTTP Kernel. To do basic route operations, you don't need to learn very much about the Symfony HTTP Kernel. Routing system in Drupal 8 is introduced by replacing the routing parts of hook_menu() in Drupal 7, and its parts, used for creating tabs, menu entries, contextual links, access arguments and actions are taken over by other sub-systems of YAML files (.yml) that provide metadata about each item and its corresponding PHP classes that serves the underlying logic. A route is a defined path, which Drupal uses to return some sort of content. For example, the default front page for Drupal, '/node' is a defined route. When Drupal receives a page request, the routing system is responsible for matching requested paths to controllers and checks the route's definition to return content. If route doesn't matches, leads to 404 (Page not found). In Drupal 7 we can use hook_menu to set up a custom page with a given path. In Drupal 8, paths are managed using a MODULE_NAME.routing.yml file to describe each route and a corresponding controller class that extends from a base controller. Each controller class have in its own file, where the file should be named to match the class name. This controller logic might have accommodated in a separate MODULE_NAME.pages.inc file in Drupal 7. A sample code in Drupal 7 might look like this: function example_menu() { $items = array(); $items['home_page'] = array( 'title' => 'Home Page', 'page callback' => example_home_page', 'access arguments' => array('access home page content'), 'type' => MENU_NORMAL_ITEM, 'file' => 'example.pages.inc' ); return $items; } function example_home_page() { return t(‘Some data can be returned.’); }In Drupal 8 we have to define the route information into a file named MODULE_NAME.routing.yml. Routes have unique names that don’t necessary have anything to do with their paths, but should be unique identifiers. They should be prefixed with your module name to keep uniqueness there by avoiding name clashes from other modules. You can use _content or _form instead of _controller in this YAML file to specify the callback. You should now always use _controller to identify the related controller and method name. example.home_page_controller: path: '/home' defaults: _controller: '\Drupal\example\Controller\HomePageController::homePage' _title: 'Home Page' requirements: _permission: 'access home page content'We now use a preceding slash on paths! In Drupal 7, the path would have been home, and in Drupal 8 /home is required! The page callback goes into a controller class, named HomePageController.php, and is located at MODULE/src/Controller/HomePageController.php. The file name need to match the class name of the controller, and all your module’s controllers should been placed in that /src/Controller directory. Drupal has adopted this location that is dictated by the PSR-4 standard. Anything that is located in the expected place in /src directory will be autoloaded when needed, without using method module_load_include() or listing file locations in the .info file, as we did in Drupal 7. The method used inside the controller to manage this route can have any name, 'homePage' is an arbitrary choice for the function in the example. The function used in the controller file should match the YAML (.yml) file, where it is described as CLASS_NAME::METHOD. The Contains line in the class @file documentation content, matches the _controller entry in the YAML file above. A controller class can manage one or more routes, as long as each has a method (function) for its callback and its own entry in the routing YAML (.yml) file. For example, the core nodeController manages four of the routes listed in node.routing.yml. Another change from Drupal 7 is that, the controller should always return a render array, not text or HTML. Drupal 8's routes includes placeholder elements which designate places where the URL contains dynamic values (slug). In the controller callback method this specified value will be available, when a variable with the same name is used in the callback method. For example see below example.routing.yml. example.name: path: '/example/{myname}' defaults: _controller: '\Drupal\example\Controller\ExampleController::content' requirements: _permission: 'access content'The {myname} element in the URL is called a slug and is available as $myname in the controller callback method. Translation is available within the controller as $this->t() instead of t(). This works because ControllerBase has added the StringTranslationTrait. /** * @file * Contains \Drupal\example\Controller\HomePageController. */ namespace Drupal\example\Controller; use Drupal\Core\Controller\ControllerBase; class HomePageController extends ControllerBase { public function homePage() { return [ '#markup' => $this->t('Something goes here!'), ]; }Some paths need additional arguments or parameters. In Drupal 7, If my page had a extra parameters, it would look like this. function example_menu() { $items = array(); $items[‘home/%/%’] = array( 'title' => 'Home Page', 'page callback' => 'example_home_page', ‘page arguments’ => array(1, 2), 'access arguments' => array('access home content'), 'type' => MENU_NORMAL_ITEM, ); return $items; } function example_home_page($first, $second) { return t(‘Something goes here’); }In Drupal 8, the YAML file will look like this (adding the additional parameters to the path): example.home_page_controller: path: '/home/{first}/{second}' defaults: _controller: '\Drupal\example\Controller\HomePageController::homePage' _title: 'Home Page’ requirements: _permission: 'access content'The controller looks like this (showing the parameters/arguments in the function signature). /** * @file * Contains \Drupal\example\Controller\HomePageController. */ namespace Drupal\example\Controller; use Drupal\Core\Controller\ControllerBase; class HomePageController extends ControllerBase { public function homePage($first, $second) { // Do something with $first and $second. return [ '#markup => $this->t('Something goes here!'), ]; } }Anything in the path could be altered by a user, so we will want to test for valid values and ensure that the values are safe to use. If the system does any sanitization of the values, or if this is a straight pass-through of whatever is in the URL. So I would probably assume that. I need to type hint and sanitize these values, for my code to work. The above code will work correctly, only for the specific path, with both parameters. Neither the path /home, nor /home/first will work, only /home/first/second. If you want the parameters to be optional, so /home, /home/first, and /home/first/second are all valid paths, you need to make some changes to the YAML(.yml) file. By adding the arguments to the defaults section of route, we are telling the controller, to treat the base path as the main route and the two additional parameters as the path alternatives. You can set default value for the parameters. The empty value says they are optional in the callback, or you could give them a fixed default value to be used if they are not present in the URL. example.home_page_controller: path: '/home/{first}/{second}' defaults: _controller: '\Drupal\example\Controller\HomePageController::homePage' _title: 'Home Page' first: '' second: '' requirements: _permission: 'access home content'Restricting Parameters After setting up parameters you should also provide information about what values will be allowed for the parameters. You can do this by adding some more information to the YAML file. The below example indicates that $first can have only the values ‘Y’ or ‘N’, and $second must be numeric. Any parameters that don’t match these rules will return a 404 error code. Basically the code is expecting to evaluate a regular expression, to determine whether the path is valid. example.home_page_controller: path: '/home/{first}/{second}' defaults: _controller: '\Drupal\example\Controller\HomePageController::homePage' _title: 'Home Page' first: '' second: '' requirements: _permission: 'access content' first: Y|N second: \d+Entity Parameters Like in Drupal 7, while creating a route, we can pass an entity id, with entity name specified, we can set the system to automatically pass the entity object to the callback instead of just the entity id. This is called upcasting. In Drupal 7, we did this by using %node instead of % alone. In Drupal 8, we just need to use the name of the entity type as the parameter name, for example, {user} or {node}. example.home_page_controller: path: '/node/{node}' defaults: _controller: '\Drupal\example\Controller\HomePageController::homePage' _title: 'Node Page' requirements: _permission: 'access content'In Drupal 8 the upcasting only happens, if we have type-hinted the entity object in controller parameter. Otherwise it will simply be the values of parameters. JSON Callbacks Render array will be converted to HTML automatically by the system. But what if you wanted that path to display JSON instead? There are some old documentation indicates that you need to add _format: json to the routing YAML file in the requirements section, but that is not required, if you do not want to provide alternate formats at the same path. Create the array of values that you want to return and then return it as a JsonResponse object. Be sure to include use Symfony\Component\HttpFoundation\JsonResponse at the top of your class. /** * @file * Contains \Drupal\example\Controller\HomePageController. */ namespace Drupal\example\Controller; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\JsonResponse; class HomePageController extends ControllerBase { public function homePage() { $return = array(); // Create key/value array. return new JsonResponse($return); } }Access Control Access control in Dripal 8 is handled by the MODULE.routing.yml file. There are many ways to control access to contents. Allow access by anyone to this path example.home_page_controller: path: '/home' requirements: _access: 'TRUE'Limit access to users with ‘access content’ permission example.home_page_controller: path: '/home' requirements: _permission: 'access content'Limit access to users with the ‘admin’ role example.home_page_controller: path: '/home' requirements: _role: 'admin'Limit access to users who have ‘edit’ permission on an entity (when the entity is provided in the path) example.home_page_controller: path: '/node/{node}' requirements: _entity_access: 'node.edit'hook_menu_alter What to do, if a route already exists (created by core or some other contributed modules) and you want to alter something about it? In Drupal 7 that is done with hook_menu_alter, but it is is also removed in Drupal 8. It’s a little more complicated now for Drupal 7 developers. A class file at MODULE/src/Routing/CLASSNAME.php location extends RouteSubscriberBase that finds the route we wants to alter using the alterRoutes() function and changes the alterations as required. /** * @file * Contains \Drupal\node\Routing\RouteSubscriber. */ namespace Drupal\node\Routing; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\RouteCollection; /** * Listens to the dynamic route events. */ class RouteSubscriber extends RouteSubscriberBase { /** * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection) { // As nodes are the primary type of content, the node listing should be // easily available. In order to do that, override admin/content to show // a node listing instead of the path's child links. $route = $collection->get('system.admin_content'); if ($route) { $route->setDefaults(array( '_title' => 'Content', '_entity_list' => 'node', )); $route->setRequirements(array( '_permission' => 'access content overview', )); } } }There is also a MODULE.services.yml file in the module to wire up the menu_alter, with an entry that points to the class that does the alterations. services: node.route_subscriber: class: Drupal\node\Routing\RouteSubscriber tags: - { name: event_subscriber }Hope this helps! Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 05th September 2016 / by wellme.m
In Drupal 8, third party libraries are downloaded and managed via composer. In Drupal each project must contain only one vendor folder. Composer Manager allows single vendor/directory shared across all modules which prevents code duplication and version mismatches by merging the requirements of all the found modules into the consolidated composer.json file. In custom module, set dependency for composer manager module in .info file. dependencies: - composer_managerWrite composer.json file for your custom module to download php library dependency. Your composer.json file should contain at least name and requirement (your php library). { "name": "drupal/mymodule", "require": { "endroid/qrcode": "^1.6" } } Following are the steps to work custom modules with composer.json : 1. First of all install Composer Manager module. 2. In your Drupal root directory, run the module's init.php script on the command line. For example, username@locahost:/var/www/SITENAME$ php modules/contrib/composer_manager/scripts/init.php 3. In command line, you will see the message after step 2 as 'Composer Manager has been successfully initialized', once initialized properly. Now run composer drupal-update from the root of your Drupal site directory. For example, username@locahost:/var/www/SITENAME$ composer drupal-update composer update After the above three steps, the php library is downloaded to the vendor folder in Drupal root directory. Now you can see the composer manager module status in /admin/reports/composer. If you want to download the dependency library in a specified folder path, then set path in your composer.json file. "config": { "vendor-dir": "includes" } Hope this helps. Also, please feel free to get in touch with us for any queries. Drupal Drupal 8 Drupal Planet Leave a reply Your email address will not be published. Required fields are marker *