/Drupal 8

function hook_filter_secure_image_alter


Alters images with an invalid source.

When the 'Restrict images to this site' filter is enabled, any images that are not hosted on the site will be passed through this hook, most commonly to replace the invalid image with an error indicator.


DOMElement $image: An IMG node to format, parsed from the filtered text.

Related topics

Define functions that alter the behavior of Drupal core.


core/modules/filter/filter.api.php, line 36
Hooks provided by the Filter module.


function hook_filter_secure_image_alter(&$image) {
  // Turn an invalid image into an error indicator.
  $image->setAttribute('src', base_path() . 'core/misc/icons/e32700/error.svg');
  $image->setAttribute('alt', t('Image removed.'));
  $image->setAttribute('title', t('This image has been removed. For security reasons, only images from the local domain are allowed.'));

  // Add a CSS class to aid in styling.
  $class = ($image->getAttribute('class') ? trim($image->getAttribute('class')) . ' ' : '');
  $class .= 'filter-image-invalid';
  $image->setAttribute('class', $class);

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.