Commit 1bb86b73 authored by neffets's avatar neffets
Browse files

fixes Issue #3285097: allow vh and wh units in width and height values, update...

fixes Issue #3285097: allow vh and wh units in width and height values, update 8203 has now a workaround for increasing the field
parent 89e05277
Loading
Loading
Loading
Loading
+65 −4
Original line number Diff line number Diff line
@@ -85,10 +85,6 @@ function iframe_update_8101(&$sandbox) {
 * Add a headerlevel column to iframe fields that do not have it yet.
 */
function iframe_update_8201(&$sandbox) {
  // Caches have to be cleared first to ensure new fields are detected in the
  // code.
  drupal_flush_all_caches();

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
  $entityFieldManager = \Drupal::service('entity_field.manager');
  $entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
@@ -133,3 +129,68 @@ function iframe_update_8201(&$sandbox) {
  }
}

/**
 * increase fields width and heighti to varchar(7), supersedes update number 8202
 */
function iframe_update_8203(&$sandbox) {
  // Caches have to be cleared first to ensure new fields are detected in the
  // code.
  drupal_flush_all_caches();

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
  $entityFieldManager = \Drupal::service('entity_field.manager');
  $entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
  $entityTypeManager = \Drupal::entityTypeManager();

  $iframeFieldMap = $entityFieldManager->getFieldMapByFieldType('iframe');
  $schema = \Drupal::database()->schema();

  // Loop through the array of iframe fields keyed by entity type...
  foreach ($iframeFieldMap as $entityTypeId => $fields) {
    foreach (array_keys($fields) as $fieldName) {
      \Drupal::messenger()->addMessage('Transform field: ' . $fieldName);
      $fieldStorageDefinition = $entityDefinitionUpdateManager->getFieldStorageDefinition($fieldName, $entityTypeId);

      // ... if the field is in a ContentEntity stored in SQL...
      $storage = $entityTypeManager->getStorage($entityTypeId);
      if ($storage instanceof SqlContentEntityStorage) {
        // ... get a map of field columns to SQL columns for that field.
        $tableMapping = $storage->getTableMapping([
          $fieldName => $fieldStorageDefinition,
        ]);

        $tableNames = $tableMapping->getDedicatedTableNames();
        $columns = $tableMapping->getColumnNames($fieldName);
        \Drupal::messenger()->addMessage('  ==> in tables: ' . implode(", ", $tableNames));

        // For each table (e.g.: data, revision), check whether the
        // 'allowfullscreen' column exists. If it does not, create it.
        foreach ($tableNames as $tableName) {
          $field_schema = $fieldStorageDefinition->getSchema();

          $fieldExists = $schema->fieldExists($tableName, $columns['width']);
          $tableExists = $schema->tableExists($tableName);

          if ($fieldExists && $tableExists) {
            $schema->changeField($tableName, $columns['width'], $columns['width'], $field_schema['columns']['width']);
            $schema->changeField($tableName, $columns['height'], $columns['height'], $field_schema['columns']['height']);
          }
        }

        // Make sure the field storage definition is updated.
        // ** normally claims changes-only-on-nodata,
        // ** Here we KNOW for sure, that the field will become larger so the data fits
        #emulate $entityDefinitionUpdateManager->updateFieldStorageDefinition($fieldStorageDefinition);
        // ignore original (old size=4 for width and height, new is greater with 7, its safe)
        $service = \Drupal::service('entity.last_installed_schema.repository');
        try {
          $service->setLastInstalledFieldStorageDefinition($fieldStorageDefinition);
        } catch (Exception $e) {
          \Drupal::messenger()->addMessage(print_r([$e->getCode(), substr($e->getMessage(), 0, 512)], true));
          \Drupal::messenger()->addMessage('ACHTUNG: If an update-error occured, please run update.php a second time.');
        }
      }
    }
  }
}
+2 −2
Original line number Diff line number Diff line
@@ -120,14 +120,14 @@ class IframeItem extends FieldItemBase {
        'width' => [
          'description' => 'The iframe width.',
          'type' => 'varchar',
          'length' => 4,
          'length' => 7,
          'not null' => FALSE,
          'default' => '600',
        ],
        'height' => [
          'description' => 'The iframe height.',
          'type' => 'varchar',
          'length' => 4,
          'length' => 7,
          'not null' => FALSE,
          'default' => '800',
        ],
+8 −8
Original line number Diff line number Diff line
@@ -83,8 +83,8 @@ class IframeWidgetBase extends WidgetBase {
      // ''
      '#default_value' => $settings['width'],
      '#description' => self::getSizedescription(),
      '#maxlength' => 4,
      '#size' => 4,
      '#maxlength' => 7,
      '#size' => 7,
    ];
    $element['height'] = [
      '#type' => 'textfield',
@@ -92,8 +92,8 @@ class IframeWidgetBase extends WidgetBase {
      // ''
      '#default_value' => $settings['height'],
      '#description' => self::getSizedescription(),
      '#maxlength' => 4,
      '#size' => 4,
      '#maxlength' => 7,
      '#size' => 7,
    ];
    $element['headerlevel'] = [
      '#type' => 'select',
@@ -310,8 +310,8 @@ class IframeWidgetBase extends WidgetBase {
      '#type' => 'textfield',
      '#default_value' => isset($settings['width']) ? $settings['width'] : '',
      '#description' => self::getSizedescription(),
      '#maxlength' => 4,
      '#size' => 4,
      '#maxlength' => 7,
      '#size' => 7,
      '#weight' => 3,
      '#element_validate' => [[$this, 'validateWidth']],
    ] + $required;
@@ -320,8 +320,8 @@ class IframeWidgetBase extends WidgetBase {
      '#title' => $this->t('Iframe Height'),
      '#default_value' => isset($settings['height']) ? $settings['height'] : '',
      '#description' => self::getSizedescription(),
      '#maxlength' => 4,
      '#size' => 4,
      '#maxlength' => 7,
      '#size' => 7,
      '#weight' => 4,
      '#element_validate' => [[$this, 'validateHeight']],
    ] + $required;