Tag: drupal

  • How to stop spam on Drupal – Webform Forms

    How to stop spam on Drupal – Webform Forms

    CleanTalk added Webform Forms spam protection using direct form integration. So if you prefer to use Webform Forms, be sure to use the most effective Anti-Spam plugin. Read the guide below and learn 7 steps to protect your Webform Forms from spam.

    Once the CleanTalk Anti-Spam plugin is installed it starts to protect all of the existing forms on your Drupal website. It may not only be Webform Forms forms but also contact forms and many others.

    Download CleanTalk Anti-Spam plugin | Download Webform Forms 

    How to install CleanTalk Anti-Spam plugin

    Go to your Anti-Spam Dashboard

    1. Download the archive on the top of the page.

    2. Go to “Manage →Extend” section, find and press button “+Install new module“.

    Drupal 9 modules install

    3. Choose the downloaded archive from CleanTalk, press the button “Install“.

    Drupal 9 install anti-spam plugin

    4. After the process of installation press the line “Enable newly added modules“.

    Drupal 9 enable anti-spam module

    5. In the “CLEANTALK” category find the Anti-Spam by CleanTalk module, tick the checkbox near the name and press the button “Install“.

    Drupal 9 enable anti-spam module

    6. When you enable the module, go to Configuration → Content Authoring. Click it.

    Drupal 9 configure anti-spam module

    Then open the Settings:

    Drupal 9 configure anti-spam module

    7. Copy the Access key from your CleanTalk Dashboard and paste it in the “Access key” field, turn on the options of the module and press the button “Save configuration“.

    Drupal 9 anti-spam plugin options

    Drupal 9 anti-spam plugin options

    How to check spam protection for Webform Forms

    Do a test registration using this blacklisted email address: st********@ex*****.com

    Drupal 9 spam registration block

    To test if SpamFireWall is working just add /?sfw_test_ip=10.10.10.10 to your site name. Example www.yoursitename.domain/?sfw_test_ip=10.10.10.10 (you must see blocking screen)

    Attention! You should test SFW with incognito mode switched on. To enable incognito mode press Ctrl+Shift+N for Chrome, Opera and Safari browsers; press Ctrl+Shift+P for Firefox, Internet Explorer and Microsoft Edge.

    How to Add a Website to Your CleanTalk Anti-Spam Dashboard
     

    Please, use this guide to add a website to your CleanTalk Anti-Spam Dashboard: https://cleantalk.org/help/add-website

    Please go to the Dashboard to see the Anti-Spam status, add new websites, or manage existing ones!

    Please check your inbox to get your account password.

    Attention!

    If you use the EU Cookie Compliance (GDPR Compliance) plugin and some settings are applied, this plugin will not allow the use of cookies for the CleanTalk Anti-Spam plugin. As a result, there may be false positive spam protection triggering.

    Drupal 8 Update Manager install

    In order to solve the situation, please add all of the cookies listed below used by the Anti-Spam plugin to the exceptions:

    ct_check_js
    ct_timezone
    ct_pointer_data
    ct_fkp_timestamp
    ct_ps_timestamp
    apbct_timestamp
    apbct_cookies_test

    Drupal 8 Update Manager install

    All CleanTalk cookies don’t contain personal data or data by which a visitor can be identified and are strictly necessary cookies as they directly affect your website security and your guests/visitors/registered users.

    That’s it! From now you know how to completely protect your Webform Forms from spam.

    If you have any questions, add a comment and we will be happy to help you.

    Create your CleanTalk account – Register now and protect your Webform Forms from spam in 5 minutes.

    Update

    The protection works only for website visitors, not for website admins. Be sure to test the form protection using Incognito mode.

    Additional features

    • CleanTalk protects all forms at once: comments, registrations, feedbacks, contacts, reviews.
    • Installation takes about 1-2 minutes.
    • Smart 99% protection against spambots.
    • Always online – 24/7 technical support.
    • Logs, SpamFireWall, personal lists, country filters, stop-words, and many others.

    Discover the complete list of CleanTalk Anti-Spam plugin features here.

  • 7 useful functions Drupal API that everyone should know!

    In this article we will look at 7 Drupal API functions that are very helpful in the development of sites to Drupal 7.

    check_plain($text) – re-encodes special characters to HTML entities.

    Parameters:

    • $text – the string for conversion

    The return value: the processed string to display as HTML.

    This function can be used to treat all kinds of data coming to the site from a variety of sources: user input, import data from another site, Twitter, etc.

    t($string, array $args = array(), array $options = array()) – converts the string to the user-selected language.

    Parameters:

    • $string – the string to be translated
    • $args – an associative array of wildcard patterns (placeholders)
    • $options – an associative array of additional options, contains two possible options: langcode – a clear indication of the language code that you want to translate a string, context – allows you to set the context of translation.

    The return value: the translated string.

    Example of the function t():

    t('Good afternoon, @first_name @last_name. ', array('@first_name' => 'Jhon', '@last_name' => 'Smith')); // Returns 'Good afternoon, Jhon Smith.'

    There are three types of wildcard patterns:

    • !name – value is substituted without processing
    • @name – value processed by the function check_plain, all HTML tags are cut
    • %name – value processed by the function theme_placeholder (also that check_plain, but the result is wrapped in a tag <em>)

    format_plural($count, $singular, $plural, array $args = array(), array $options = array()) – creates a string containing quantitative value.

    Parameters:

    • $count – quantitative value
    • $singular – a string that will be used if $count == 1
    • $plural – a string that will be used if $count > 1
    • $args – an associative array of of wildcard patterns (placeholders)
    • $options – the same as in the t() function

    The return value: a string translated by using the function t(), depending on the parameter $count selected string that will be used for translation.

    Example:

    $comment_count=1;
    format_plural($comment_count, '1 comment', '@count comments'); // return '1 comment'
    
    $comment_count=5;
    format_plural($comment_count, '1 comment', '@count comments'); // return '5 comments'

    drupal_get_title() – returns the current page title.

    This function can be used in combination with drupal_set_title() to process the header and install a new one.

    drupal_set_title($title = NULL, $output = CHECK_PLAIN) – sets the title of the page.

    Parameters:

    • $title – a string that will be used as the title of the page
    • $output – a flag that determines whether the $title processed by function check_plain().

    The return value: updated page title.

    url($path = NULL, array $options = array()) – forms an internal or external URL.

    Parameters:

    • $ path – internal relative or external absolute path
    • $ options – an associative array of options:
      • query- array passed parameters such as key/value
      • fragment – the anchor element on the page
      • absolute – flag (default FALSE) if set to TRUE then the url will be defined as an absolute.
      • alias – the flag (default FALSE) if set to TRUE then the path will be regarded as an alias (this will not be accomplished in the search for the alias database that will speed up)
      • external – the flag, if set to TURE url will be regarded as external.
      • language – a language object defines the language to find an alias selected language
      • https – the flag is set to TRUE if the path will be https protocol, if FALSE, then the http.
      • base_url – the value to replace the standard base path
      • prefix – the prefix path language

    The returned value: formed URL.

    drupal_goto($path = ”, array $options = array(), $http_response_code = 302) – produces redirect the user to another page.

    Parameters:

    • $path – relative or absolute path to be produced redirect
    • $options – the list of options as a function of the url()
    • $http_response_code – code status code

    This text is a translation of the article “7 полезных функций Drupal API который должен знать каждый!” published on drupal-learning.com.

    Forums and blogs without spam

    CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.

  • A few tips to accelerate Drupal

    CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.

    Drupal constantly criticized for its slowness, for the great number of database requests and clumsiness. Most often it is solved by using Memcached or Varnish. In this article I would like to add a few more tips, the use of which will not make Drupal even slower. Those who are interested, please welcome under the cut.

    Use JS module

    Together with the functionality of the site is growing and the number of consumed memory and the number of SQLrequests needed to perform a full load Drupal. If you need to do only one SQL request using AJAX, Drupal can spend a lot of time to fully load and execute code that may never be used in the request. JS module solves this problem by providing an alternative way to download Drupal only to the level required for a specific task. Including allowing you to connect the necessary files and modules to handle the request.

    Drupal loads itself with each request through a series of phases of the boot. All phases of the download are defined in the file bootstarp.inc:

    • DRUPAL_BOOTSTRAP_CONFIGURATION: In this phase filled an internal array of configurations Drupal, set the base URL, analyze settings.php file, etc.
    • DRUPAL_BOOTSTRAP_PAGE_CACHE: An attempt to provide a page from the cache, if enabled page caching for anonymous users.
    • DRUPAL_BOOTSTRAP_DATABASE: Determine the type of database and the connection to request the database.
    • DRUPAL_BOOTSTRAP_VARIABLES: Initialization of variables.
    • DRUPAL_BOOTSTRAP_SESSION: Initialization treatment of sessions.
    • DRUPAL_BOOTSTRAP_PAGE_HEADER: Setting the page title.
    • DRUPAL_BOOTSTRAP_LANGUAGE: Definition of the language of the page.
    • DRUPAL_BOOTSTRAP_FULL: Loading modules and initialization of issues.

    For example, if you only need to use the function variable_get () in the AJAX callback will be enough level DRUPAL_BOOTSTRAP_VARIABLES, and if you need access to the current user object $user you need to use DRUPAL_BOOTSTRAP_SESSION etc.

    To work with the module JS enough to realize hook_js (), in which to describe what modules you need to connect, what phase of the bootstrap to use:

    /**
     * Implements hook_js().
     */
    function js_example_js() {
      return array(
        'results' => array(
          'callback' => 'js_example_ajax_results', // The callback function is required to display the results
          'includes' => array('unicode', 'locale', 'language'), // Which files to download from a directory /includes
          'dependencies' => array(), // List of modules to be loaded
          'bootstrap' => DRUPAL_BOOTSTRAP_DATABASE, // Required loading phase Drupal
          'file' => 'js_example.ajax.inc', // File that contains the callback
          'access callback' => 'js_example_ajax_results_access', // The callback function to check the access rights
          'access arguments' => array(), // The argument list for the access callback
          'page arguments' => array(), // The argument list for the callback
          'skip_hook_init' => TRUE, // A flag that allows you to skip the execution hook_init(),
          'i18n' => FALSE, // A flag that lets you enable or disable support for i18n
        ),
      );
    }
    

    It is important to understand that it is quite difficult to perform the control of access rights in the initial stages of loading Drupal, so it is necessary to closely monitor the security of their code.

    Loading entities

    It is often necessary to add fields from the user profile in the node and the most simple solution for this use the hook template_preprocess_node():

    template_preprocess_node(&$variables){
    $node = $variables['node'];
    $variables['account'] = user_load($node->uid);
    }

    But when displaying a large number of nodes, this approach will create a large number of database requests. Get the same functionality without sacrificing performance by using the hook hook_entity_prepare_view():

    hook_entity_prepare_view($entities, $type, $langcode{
      if ($type != 'node') {
        return;
      }
      $uids = array();
      foreach ($entities as $entity) {
        $uids[] = $entity->uid;
      }
      $accounts = user_load_multiple($uids);
      foreach ($entities as $entity) {
        $entity->account = $accounts[$entity->uid];
      }
    }
    

    After that, $ entity-> account will be available in preprotsesse:

    template_preprocess_node(&$vars) {
      $account = $vars['node']->account;
    }
    

    Use drupal_static()

    When the code is executed multiple times during a single request, it is very convenient to use static variables to cache (more on static variables in PHP can be found here and here). Drupal core provides an excellent solution for the realization of static caching – function drupal_static(). Function drupal_static() provides a central static variable to store data. The first call drupal_static () returns NULL, but any changes in this variable will be saved for the next call to this function. This way we can check whether the variable is already installed and get it instantly without doing almost no work at the same time.

    function my_module_function() {
      $foo = &drupal_static(__FUNCTION__);
      global $user;
      if (!isset($foo[$user->uid])) {
        $foo[$user->uid] = something_expensive();
      }
      return $foo[$user->uid];
    }
    

    Frequent use of variable_set () affect performance

    Variables in Drupal are stored in a special table in the format: name – the serialized value. For each request, all variables are loaded from the cache in the global variable $conf.

    During saving of each variable the following happens:

    • The record is updated in the database
    • Ceared cache
    • When the next request finds that there is no cache for table variables, all variables are loaded and stored in the cache.

    With a large number of variables it can take a lot of time. In Drupal is realized locking system and any long-running operations in parallel which, most likely, will come other requests should try before work to get a lock. If the previous request to clear the cache variables, the following request will rebuild it, so it is very frequent use functions variable_set() can lead to mass table lock, due to the fact that tens of requests waiting for a new record cache table of variables that can become out of date before it will extract for use.

    Reduction of the table sessions

    Drupal stores user sessions in the database instead of files, so if a site with a large attendance, this table can quickly grow to enormous size. If the table session was very large, you can increase the frequency of garbage collection for PHP-sessions in settings.php:

    ini_set('session.gc_maxlifetime', 86400); 
    

    I hope that not all of this list is just too obvious things and this is useful to someone.

    This text is a translation of the article “Несколько советов по ускорению Drupal” by afi13 published on habrahabr.ru.

    Forums and blogs without spam

    CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.