You are here

function photos_update_8501 in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x photos.install \photos_update_8501()

Prep for new photos_image entity.

File

./photos.install, line 120
Install, update, and uninstall functions for the Photos module.

Code

function photos_update_8501(&$sandbox) {

  // Check if this update needs to run.
  if (\Drupal::database()
    ->schema()
    ->fieldExists('photos_image', 'fid')) {

    // Move photos_image to photos_image_tmp.
    // @see photos_post_update_migrate_photos_image_entity_1().
    \Drupal::database()
      ->schema()
      ->renameTable('photos_image', 'photos_image_tmp');

    // Rename {photos_album} fields.
    $album_id_field = [
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'pid', 'album_id', $album_id_field);
    $cover_id_field = [
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'fid', 'cover_id', $cover_id_field);
    $weight_field = [
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'wid', 'weight', $weight_field);

    // Create new photos_image entity.
    $definition_update_manager = Drupal::entityDefinitionUpdateManager();

    // Basically the entity plugin definition for the custom entity, converted
    // to an associative array.
    $entity_definition = new ContentEntityType([
      "id" => "photos_image",
      "label" => new TranslatableMarkup("Photo"),
      "label_collection" => new TranslatableMarkup("Photos"),
      "label_singular" => new TranslatableMarkup("photo"),
      "label_plural" => new TranslatableMarkup("photos"),
      "label_count" => new PluralTranslation([
        "singular" => "@count photo",
        "plural" => "@count photos",
      ]),
      "handlers" => [
        "storage" => "Drupal\\photos\\PhotosImageStorage",
        "storage_schema" => "Drupal\\photos\\PhotosImageStorageSchema",
        "form" => [
          "default" => "Drupal\\photos\\Form\\PhotosImageEditForm",
          "add" => "Drupal\\photos\\Form\\PhotosImageAddForm",
          "edit" => "Drupal\\photos\\Form\\PhotosImageEditForm",
          "delete" => "Drupal\\photos\\Form\\PhotosImageDeleteForm",
          "delete-multiple-confirm" => "Drupal\\Core\\Entity\\Form\\DeleteMultipleForm",
        ],
        "access" => "Drupal\\photos\\PhotosAccessControlHandler",
        "views_data" => "Drupal\\photos\\PhotosViewsData",
        "list_builder" => "Drupal\\photos\\PhotosImageListBuilder",
        "route_provider" => [
          "html" => "Drupal\\photos\\Entity\\PhotosRouteProvider",
        ],
      ],
      "base_table" => "photos_image",
      "data_table" => "photos_image_field_data",
      "revision_table" => "photos_image_revision",
      "revision_data_table" => "photos_image_field_revision",
      "translatable" => TRUE,
      "show_revision_ui" => TRUE,
      "entity_keys" => [
        "id" => "id",
        "revision" => "revision_id",
        "label" => "title",
        "langcode" => "langcode",
        "uuid" => "uuid",
        "status" => "status",
        "published" => "status",
        "uid" => "uid",
        "owner" => "uid",
      ],
      "revision_metadata_keys" => [
        "revision_user" => "revision_user",
        "revision_created" => "revision_created",
        "revision_log_message" => "revision_log_message",
      ],
      "admin_permission" => "administer nodes",
      "common_reference_target" => TRUE,
      "field_ui_base_route" => "photos.admin",
      "links" => [
        "canonical" => "/photos/{node}/{photos_image}",
        "add-form" => "/photos/image/add",
        "delete-form" => "/photos/{node}/{photos_image}/delete",
        "edit-form" => "/photos/{node}/{photos_image}/edit",
        "version-history" => "/photos/{node}/{photos_image}/revisions",
        "revision" => "/photos/{node}/{photos_image}/revisions/{photos_image_revision}/view",
      ],
    ]);

    // Finally you need to manually set the class to where the entity plugin
    // definition is.
    $entity_definition
      ->setClass('Drupal\\photos\\Entity\\PhotosImage');

    // Create new entity type.
    $definition_update_manager
      ->installEntityType($entity_definition);

    // Add photos_image field.
    // @todo update field path settings based on existing path settings.
    // @todo replace old field path tokens with new tokens.
    // @todo add field display and form display.
    $field_storage = FieldStorageConfig::loadByName('photos_image', 'field_image');
    $field = FieldConfig::loadByName('photos_image', 'photos_image', 'field_image');
    if (empty($field)) {
      if (empty($field_storage)) {
        $field_storage = FieldStorageConfig::create([
          'entity_type' => 'photos_image',
          'field_name' => 'field_image',
          'type' => 'image',
          'settings' => [
            'target_type' => 'file',
          ],
          'cardinality' => 1,
        ]);
        $field_storage
          ->save();
      }
      $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'photos_image',
        'label' => 'Image',
        'settings' => [
          'file_directory' => 'photos/images',
          'file_extensions' => 'png gif jpg jpeg',
        ],
      ]);
      $field
        ->save();
    }
  }
}