class ImageStyleStorageController

Defines a controller class for image styles.

Hierarchy

Expanded class hierarchy of ImageStyleStorageController

File

drupal/core/modules/image/lib/Drupal/image/ImageStyleStorageController.php, line 18
Contains \Drupal\image\ImageStyleStorageController.

Namespace

Drupal\image
View source
class ImageStyleStorageController extends ConfigStorageController {

  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::attachLoad().
   */
  protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
    foreach ($queried_entities as $style) {
      if (!empty($style->effects)) {
        foreach ($style->effects as $ieid => $effect) {
          $definition = image_effect_definition_load($effect['name']);
          $effect = array_merge($definition, $effect);
          $style->effects[$ieid] = $effect;
        }

        // Sort effects by weight.
        uasort($style->effects, 'drupal_sort_weight');
      }
    }
    parent::attachLoad($queried_entities, $revision_id);
  }

  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::postSave().
   */
  protected function postSave(EntityInterface $entity, $update) {
    if ($update && !empty($entity->original) && $entity->{$this->idKey} !== $entity->original->{$this->idKey}) {

      // The old image style name needs flushing after a rename.
      image_style_flush($entity->original);

      // Update field instance settings if necessary.
      $this
        ->replaceImageStyle($entity);
    }
  }

  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::postDelete().
   */
  protected function postDelete($entities) {
    foreach ($entities as $style) {

      // Flush cached media for the deleted style.
      image_style_flush($style);

      // Check whether field instance settings need to be updated.
      // In case no replacement style was specified, all image fields that are
      // using the deleted style are left in a broken state.
      if ($new_id = $style
        ->get('replacementID')) {

        // The deleted ID is still set as originalID.
        $style
          ->set('name', $new_id);
        $this
          ->replaceImageStyle($style);
      }
    }
  }

  /**
   * Update field instance settings if the image style name is changed.
   *
   * @param ImageStyle $style
   *   The image style.
   */
  protected function replaceImageStyle(ImageStyle $style) {
    if ($style
      ->id() != $style
      ->getOriginalID()) {
      $instances = field_read_instances();

      // Loop through all fields searching for image fields.
      foreach ($instances as $instance) {
        if ($instance
          ->getField()->type == 'image') {
          $view_modes = entity_get_view_modes($instance['entity_type']);
          $view_modes = array(
            'default',
          ) + array_keys($view_modes);
          foreach ($view_modes as $view_mode) {
            $display = entity_get_display($instance['entity_type'], $instance['bundle'], $view_mode);
            $display_options = $display
              ->getComponent($instance['field_name']);

            // Check if the formatter involves an image style.
            if ($display_options && $display_options['type'] == 'image' && $display_options['settings']['image_style'] == $style
              ->getOriginalID()) {

              // Update display information for any instance using the image
              // style that was just deleted.
              $display_options['settings']['image_style'] = $style
                ->id();
              $display
                ->setComponent($instance['field_name'], $display_options)
                ->save();
            }
          }
          $entity_form_display = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default');
          $widget_configuration = $entity_form_display
            ->getComponent($instance['field_name']);
          if ($widget_configuration['settings']['preview_image_style'] == $style
            ->getOriginalID()) {
            $widget_options['settings']['preview_image_style'] = $style
              ->id();
            $entity_form_display
              ->setComponent($instance['field_name'], $widget_options)
              ->save();
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigStorageController::$configFactory protected property The config factory service.
ConfigStorageController::$configStorage protected property The config storage service.
ConfigStorageController::$statusKey protected property Name of the entity's status key or FALSE if a status is not supported.
ConfigStorageController::$uuidKey protected property Name of the entity's UUID property. Overrides EntityStorageControllerBase::$uuidKey
ConfigStorageController::buildQuery protected function Builds the query to load the entity.
ConfigStorageController::create public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::create(). Overrides EntityStorageControllerInterface::create 2
ConfigStorageController::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance 2
ConfigStorageController::delete public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::delete(). Overrides EntityStorageControllerInterface::delete
ConfigStorageController::deleteRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::deleteRevision(). Overrides EntityStorageControllerInterface::deleteRevision
ConfigStorageController::getConfigPrefix public function Returns the config prefix used by the configuration entity type.
ConfigStorageController::getFieldDefinitions public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getFieldDefinitions(). Overrides EntityStorageControllerInterface::getFieldDefinitions
ConfigStorageController::getIDFromConfigName public static function Extracts the configuration entity ID from the full configuration name.
ConfigStorageController::getQueryServicename public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getQueryServicename(). Overrides EntityStorageControllerInterface::getQueryServicename
ConfigStorageController::importCreate public function Create configuration upon synchronizing configuration changes. 1
ConfigStorageController::importDelete public function Delete configuration upon synchronizing configuration changes. 2
ConfigStorageController::importUpdate public function Updates configuration upon synchronizing configuration changes. 1
ConfigStorageController::invokeHook protected function Invokes a hook on behalf of the entity.
ConfigStorageController::load public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::load(). Overrides EntityStorageControllerInterface::load 2
ConfigStorageController::loadByProperties public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadByProperties(). Overrides EntityStorageControllerInterface::loadByProperties 2
ConfigStorageController::loadRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision(). Overrides EntityStorageControllerInterface::loadRevision
ConfigStorageController::preDelete protected function Acts on entities before they are deleted. 3
ConfigStorageController::preSave protected function Acts on an entity before the presave hook is invoked. 6
ConfigStorageController::save public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::save(). Overrides EntityStorageControllerInterface::save
ConfigStorageController::__construct public function Constructs a ConfigStorageController object. Overrides EntityStorageControllerBase::__construct 2
EntityStorageControllerBase::$cache protected property Whether this entity type should use the static cache. 1
EntityStorageControllerBase::$entityCache protected property Static cache of entities.
EntityStorageControllerBase::$entityInfo protected property Array of information about the entity.
EntityStorageControllerBase::$entityType protected property Entity type for this controller instance.
EntityStorageControllerBase::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
EntityStorageControllerBase::$idKey protected property Name of the entity's ID field in the entity database table.
EntityStorageControllerBase::cacheGet protected function Gets entities from the static cache.
EntityStorageControllerBase::cacheSet protected function Stores entities in the static entity cache.
EntityStorageControllerBase::loadUnchanged public function Loads an unchanged entity from the database. Overrides EntityStorageControllerInterface::loadUnchanged
EntityStorageControllerBase::resetCache public function Resets the internal, static entity cache. Overrides EntityStorageControllerInterface::resetCache 3
ImageStyleStorageController::attachLoad protected function Overrides \Drupal\Core\Config\Entity\ConfigStorageController::attachLoad(). Overrides ConfigStorageController::attachLoad
ImageStyleStorageController::postDelete protected function Overrides \Drupal\Core\Config\Entity\ConfigStorageController::postDelete(). Overrides ConfigStorageController::postDelete
ImageStyleStorageController::postSave protected function Overrides \Drupal\Core\Config\Entity\ConfigStorageController::postSave(). Overrides ConfigStorageController::postSave
ImageStyleStorageController::replaceImageStyle protected function Update field instance settings if the image style name is changed.