Showing posts with label Drupal 8. Show all posts
Showing posts with label Drupal 8. Show all posts

Wednesday, May 8, 2019

Mapping aditional attributes coming from SimpleSAML ADFS


For example , consider if ADFS SSO giving additional parameters like first name, surname, job title, etc., so how we map these additional attributes?
The 'simplesamlphp_auth' module gives us the option to map username, email, etc., from its user sync configuration page, buts it's not sufficient to map all fields. So here I would like to mention a way to map the additional fields.

There is a hook available for this.

hook_simplesamlphp_auth_user_attributes

I have a module named general in my code path. So I will write the above hook like below.

<?php
/**
 * Mapping of additional parameters/SAML attributes to Drupal user profile fields.
 * @param \Drupal\user\UserInterface $account
 *   The Drupal account/user
 * @param array $attributes
 *   The SimpleSAMLphp attributes for this account.
 */

function general_simplesamlphp_auth_user_attributes(\Drupal\user\UserInterface $account, $attributes) {
  $first_name = $attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'];
  $sur_name = $attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname'];
  $job_title = $attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/jobtitle'];

  $account->set('field_first_name', $saml_first_name);
  $account->set('field_last_name', $sur_name); 
  $account->set('field_job_title', $job_title);
  return $account;

}
?>

SSO Implementation using SimpleSAML - in Drupal


A great tutorial here : https://www.chapterthree.com/blog/how-to-configure-simplesamlphp-for-drupal-8-on-acquia

After successfully setting the library and modules you have to send the metadata to the ADFS provider.
They will send you an XML adfs metadata.
You need to convert the XML metadata into PHP array, for that please go to the simplesaml configuration page. Which is normally http://www.examplesite.com/simplesaml
Go to Federation tab
Click the link labelled 'XML to SimpleSAMLphp metadata converter'. Which will ask you to log-in(if you are not logged in)
Paste the XML metadata/or upload the XML file here and click parse, you will get the required PHP array
Copy and paste the content in /simplesamlphp/metadata/saml20-idp-remote.php

Monday, February 25, 2019

Content export to CSV, JSON or XML

1. Download and install Views Data Export Module (https://www.drupal.org/project/views_data_export)

If you are using composer to install the module, then all the dependencies will automatically be downloaded.  Otherwise, follow the below steps

1.a. Download and install the CSV Serialization module(https://www.drupal.org/project/csv_serialization), this module requires a library League\Csv. So if you trying to install this module you may get below error message

CSV serialization requires the League\Csv library.


1.b To fix the above issue, download and install Ludwig module (https://www.drupal.org/project/ludwig) It provides a manual alternative to Composer.

After installing Ludwig, go to reports > packages.

Follow the instructions mentioned on that page. You may be instructed to download the library and copy to a specified folder. Do as per that then,

1.c Install CSV Serialization and Views Data export modules respectively.

After successful installation,

Follow the instruction mentioned here (https://www.drupal.org/node/1820452).

Wednesday, February 13, 2019

Drupal 8 Interview questions


  1. What are the custom entities? When and how it can be used?https://opensenselabs.com/blog/tech/how-create-custom-entity-drupal-8
  2. How to create a patch?
  3. Mysql rollback to flag
  4. What are cache tags in Drupal 8?
    https://www.drupal.org/docs/8/api/cache-api/cache-tags
  5. How to create a module using Drupal console?
  6. How to deploy code - Acquia
  7. Custom entities into the single table
  8. Why we need an interface?
    An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy. An interface enables you to model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.
  9. How to pass the interface variable in the constructor
  10. How to make a string to upper case in Twig
    To make a string in upper case use filter 'upper'
    {{ 'a string'|upper }}
  11. How to create a custom pipe.
    https://symfony.com/doc/current/templating/twig_extension.html
  12. What happens behind when the twig filter used?

Tuesday, January 29, 2019

How to create a subsite in Drupal


  1. Create a subdirectory inside sites folder of your Drupal installation, it will be inside docroot folder.
    Drupal-Multisite Directory Structure
  2. Create a virtual host. You can follow any of the steps from traditional way to executing a bash script. I recommend executing a bash script so that you don't need to go through all steps everytime.
  3. Make a copy of /docroot/sites/example.sites.php called /docroot/sites/sites.php
  4. Add the below line of code inside the sites.php.
    $sites['sub-site-address.domain'] = 'subsite';
  5. Check the newly created subsite from your browser 'sub-site-address.domain' and install the site.
  6. Create folders for modules, themes, libraries etc., and add your custom theme or module specific to this newly created site if you wish.

Tuesday, January 15, 2019

Show previous chapter, next chapter navigation in book page of Drupal 8.

Q: I want to show a navigation to previous chapter & next chapter in my book detail page(node view). I'll explain the scenario: I have a parent book and multiple child pages, each may have children up-to level n. I'm showing the level 1(depth 2) pages in a single view(details page) and the children in same page. Here comes the navigation part. I want to show a navigation to previous and next chapters in the Book details page.

A: After creation of the Book page details page, create a view as per the below pictures

1.Create a View-Give name and select the content type. Check 'Create a Block'.

2. As per the scenario I want to see Level 2 book pages(siblings of current page), so I set Depth is equal to 2 under Filter criteria. Sort criteria set us Depth's Ascending order.
3. Created a contextual filter: Node ID and set its default value from the URL(as we are viewing the node view page)
4. Under more we have to check the exclude option so that the result will exclude the current book page.

5.Save the view. See I created two blocks , one for previous link and one for next link. 
By visiting structure > Block Layout you are able to place the newly created blocks.

 6. Now we have to create a module to apply some query conditions to the view. Simply create a module and paste the below code in your module's .module file and enable it.

<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
/**
 * To get the next and previous chapters
 *
 * @param ViewExecutable $view
 * @param QueryPluginBase $query
 * @return void
 */
function chapter_navigation_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {   
  
  $node = \Drupal::routeMatch()->getParameter('node');
  
  if ($view->id() == 'chapter_navigation' && $node) {
    $operator = $view->getDisplay()->display['id'] == 'next_chapter' ? '>=' : '<=';
    foreach ($query->where as &$condition_group) {
      array_push($condition_group['conditions'], [
        'field' => 'book.weight '.$operator.' ' . $node->book['weight'],
        'value' => [],
        'operator' => 'formula',

      ]);

    }   
 
  }

}

Tuesday, May 7, 2013

Show child pages in parent book details view page in Drupal 8

Q: I'm using the Book module to maintain the parent-child relationship between content. I want to see all child pages expanded when I see a parent node details page. How can I achieve it?

I tried with THEMENAME_preprocess_book_tree(), but so far I have not been able to achieve that I wanted.
Is this the right hook to use in my case?

A: I managed to find a solution. I would like to post it here if somebody looking for similar solutions.

Make a view for the parent page with all needed configurations. In my case, I have the main chapter page as the parent, the content type is 'chapter'. And when I see the view in a page I want to see all sub-chapters coming under it. The further steps are followed:

  1. Browse to /admin/structure/views and click on "Add view".
  2. Enter a view name, Select content type from view settings
  3. Check the "Create a block" checkbox only. Then click on "Save & edit".
  4. On the right-hand side, click on "Advanced" so that the advanced menu appears.
  5. Click on "add" next to "Relationships" to add a relationship.
  6. Select "1st parent" (the parent book node) and click on "Apply and configure relationships".
  7. Check the "Require this relationship" checkbox and click on "Apply".
  8. Click on "add" next to "Contextual filters" to add a contextual filter.
  9. Select "ID" (The node ID) and click on "Add and configure contextual filters". Choose relationship to "Book 1st Parent"
  10. In the "when the filter value is not available" section, select "Provide default value" and in the "Type" drop-down menu, select "Content ID from URL"
  11. click on "Apply" & Save the view
  12. Place the just created block in the page by visiting structure > block layout