You are here

class DefaultCropHandler in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/cms_content_sync/entity_handler/DefaultCropHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultCropHandler
  2. 2.1.x src/Plugin/cms_content_sync/entity_handler/DefaultCropHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultCropHandler

Class DefaultCropHandler, providing a minimalistic implementation for the crop entity type.

Plugin annotation


@EntityHandler(
  id = "cms_content_sync_crop_handler",
  label = @Translation("Default Crop"),
  weight = 90
)

Hierarchy

Expanded class hierarchy of DefaultCropHandler

File

src/Plugin/cms_content_sync/entity_handler/DefaultCropHandler.php, line 22

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler
View source
class DefaultCropHandler extends EntityHandlerBase {
  public const USER_REVISION_PROPERTY = 'revision_uid';

  /**
   * {@inheritdoc}
   */
  public static function supports($entity_type, $bundle) {
    return 'crop' == $entity_type;
  }

  /**
   * {@inheritdoc}
   */
  public function getHandlerSettings($current_values, $type = 'both') {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPushOptions() {
    return [
      PushIntent::PUSH_DISABLED,
      PushIntent::PUSH_AS_DEPENDENCY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPullOptions() {
    return [
      PullIntent::PULL_DISABLED,
      PullIntent::PULL_AS_DEPENDENCY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function pull(PullIntent $intent) {

    /** @var FileInterface[] $files */
    $files = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->loadByProperties([
      'uri' => $intent
        ->getProperty('uri')[0]['value'],
    ]);

    // Unset the entity_id field so that it gets automatically set during
    // the entity creation.
    if (!count($files)) {
      $intent
        ->overwriteProperty('entity_id', null);
    }
    else {

      /**
       * @var \Drupal\file\FileInterface $file
       */
      $file = reset($files);
      $this
        ->deleteExistingCrop($intent, $file);
      $intent
        ->overwriteProperty('entity_id', $file
        ->id());
    }
    if (!parent::pull($intent)) {
      return false;
    }
    if (!count($files)) {
      MissingDependencyManager::saveUnresolvedDependency('file', $intent
        ->getProperty('uri')[0]['value'], $intent
        ->getEntity(), $intent
        ->getReason(), 'entity_id');
    }
    return true;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPreviewOptions() {
    return [
      'table' => 'Table',
      'preview_mode' => 'Preview mode',
    ];
  }
  public function getViewUrl(EntityInterface $entity) {
    $uri = $entity
      ->get('uri')
      ->getValue()[0]['value'];

    /** @var FileInterface[] $files */
    $files = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->loadByProperties([
      'uri' => $uri,
    ]);
    if (empty($files)) {
      return \Drupal::urlGenerator()
        ->generateFromRoute('<front>', [], [
        'absolute' => true,
      ]);
    }
    $file = reset($files);
    $uri = $file
      ->getFileUri();
    return \Drupal\Core\Url::fromUri(file_create_url($uri))
      ->toString();
  }

  /**
   * Delete already existing Crop entity.
   *
   * @param $file
   */
  private function deleteExistingCrop(PullIntent $intent, $file) {
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler
      ->moduleExists('crop')) {
      $crop = Crop::findCrop($file
        ->getFileUri(), $intent
        ->getBundle());
      if ($crop && $crop
        ->uuid() != $intent
        ->getUuid()) {
        $crop
          ->delete();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultCropHandler::deleteExistingCrop private function Delete already existing Crop entity.
DefaultCropHandler::getAllowedPreviewOptions public function Overrides EntityHandlerInterface::getAllowedPreviewOptions
DefaultCropHandler::getAllowedPullOptions public function Get the allowed pull options. Overrides EntityHandlerBase::getAllowedPullOptions
DefaultCropHandler::getAllowedPushOptions public function Get the allowed push options. Overrides EntityHandlerBase::getAllowedPushOptions
DefaultCropHandler::getHandlerSettings public function Get the handler settings. Overrides EntityHandlerBase::getHandlerSettings
DefaultCropHandler::getViewUrl public function Overrides EntityHandlerBase::getViewUrl
DefaultCropHandler::pull public function Pull the remote entity. Overrides EntityHandlerBase::pull
DefaultCropHandler::supports public static function Check if this handler supports the given entity type. Overrides EntityHandlerInterface::supports
DefaultCropHandler::USER_REVISION_PROPERTY public constant Overrides EntityHandlerBase::USER_REVISION_PROPERTY
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$bundleName protected property
EntityHandlerBase::$entityTypeName protected property
EntityHandlerBase::$flow protected property A sync instance.
EntityHandlerBase::$logger protected property A logger instance.
EntityHandlerBase::$settings protected property
EntityHandlerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntityHandlerBase::createNew protected function 1
EntityHandlerBase::deleteEntity protected function Delete a entity.
EntityHandlerBase::getDateProperty protected function
EntityHandlerBase::getEntityName protected function
EntityHandlerBase::getForbiddenFields public function Provide a list of fields that are not allowed to be pushed or pulled. These fields typically contain all label fields that are pushed separately anyway (we don't want to set IDs and revision IDs of entities for example, but only use the UUID for… Overrides EntityHandlerInterface::getForbiddenFields 4
EntityHandlerBase::getStaticFields protected function Get a list of fields that can't be updated.
EntityHandlerBase::hasLabelProperty protected function Check whether the entity type supports having a label. 2
EntityHandlerBase::ignorePull protected function Check if the pull should be ignored. 2
EntityHandlerBase::ignorePush protected function Check if the entity should not be ignored from the push. 2
EntityHandlerBase::isEntityTypeTranslatable protected function
EntityHandlerBase::push public function Overrides EntityHandlerInterface::push 5
EntityHandlerBase::pushReferencedMenuItems protected function Whether or not menu item references should be pushed.
EntityHandlerBase::REVISION_TRANSLATION_AFFECTED_PROPERTY public constant 2
EntityHandlerBase::saveEntity protected function 1
EntityHandlerBase::setDateProperty protected function
EntityHandlerBase::setEntityValues protected function Set the values for the pulled entity. 2
EntityHandlerBase::setPreviewHtml protected function
EntityHandlerBase::updateEntityTypeDefinition public function Update the entity type definition. Overrides EntityHandlerInterface::updateEntityTypeDefinition 3
EntityHandlerBase::USER_PROPERTY public constant 2
EntityHandlerBase::validateHandlerSettings public function Validate the settings defined above. $form and $form_state are the same as in the Form API. $settings_key is the index at $form['sync_entities'] for this handler instance. Overrides EntityHandlerInterface::validateHandlerSettings
EntityHandlerBase::__construct public function Constructs a Drupal\rest\Plugin\ResourceBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.