You are here

public function AmpMetadataForm::save in Accelerated Mobile Pages (AMP) 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/AmpMetadataForm.php, line 306

Class

AmpMetadataForm
Class AmpMetadataForm.

Namespace

Drupal\amp\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var AmpMetadata $amp_metadata */
  $amp_metadata = $this->entity;
  $this->tagInvalidate
    ->invalidateTags([
    'amp_metadata',
  ]);
  if ($amp_metadata
    ->isNew()) {
    if (!$this->ampMetadataInfo
      ->ampMetadataHasGlobal()) {
      $amp_metadata
        ->setGlobal();
      $amp_metadata
        ->set('id', 'global');
      $amp_metadata
        ->set('label', $this
        ->t('Global settings for AMP metadata'));
    }
    else {
      $node_type = $form_state
        ->getValue('node_type');
      $amp_metadata
        ->setNodeType($node_type);
      $amp_metadata
        ->set('id', $node_type);
      $amp_metadata
        ->set('label', $this
        ->t('@type settings for AMP metadata', [
        '@type' => $this->entityTypeManager
          ->getStorage('node_type')
          ->load($node_type)
          ->label(),
      ]));
    }
    $this->tagInvalidate
      ->invalidateTags([
      'amp_available_metadata',
    ]);
  }

  // Save organization name.
  $amp_metadata
    ->setOrganizationName($form_state
    ->getValue('amp_organization_name'));

  // Get file IDs of the currently uploaded logo as well as the previously
  // uploaded logo.
  $logo_fid_new = '';
  $logo_value_new = $form_state
    ->getValue('amp_organization_logo_fid_new');
  if (!empty($logo_value_new) && isset($logo_value_new[0])) {
    $logo_fid_new = $logo_value_new[0];
  }
  $logo_fid_previous = '';
  $logo_value_previous = $form_state
    ->getValue('amp_organization_logo_fid_previous');
  if (!empty($logo_value_previous) && isset($logo_value_previous[0])) {
    $logo_fid_previous = $logo_value_previous[0];
  }

  // Save new organization logo.

  /** @var \Drupal\file\FileInterface $logo_file_new */
  if (!empty($logo_fid_new) && !empty($logo_file_new = File::load($logo_fid_new))) {
    $logo_file_new
      ->setPermanent();
    $logo_file_new
      ->save();

    // File usage requires an entity type and entity ID. Those don't exist
    // for simple configuration: we use organization and the fid for those
    // items. This causes an error if you go to the file usage page, as the
    // organization entity type does not exist. Life is full of hard choices
    // like this. Alternative is using config entity, which seems unwieldy.
    $this->fileUsage
      ->add($logo_file_new, 'amp', 'amp_metadata', $amp_metadata
      ->get('id'));
    $amp_metadata
      ->setOrganizationLogoFid($logo_fid_new);
  }

  // If no file ID is set for the logo but a value previously existed, or if
  // the new file ID does not match the previous file ID, then the previous
  // file needs to be removed. Delete the previous logo file.

  /** @var \Drupal\file\FileInterface $logo_file_previous */
  if (!empty($logo_fid_previous) && $logo_fid_previous !== $logo_fid_new && !empty($logo_file_previous = File::load($logo_fid_previous))) {
    $this->fileUsage
      ->delete($logo_file_previous, 'amp', 'amp_metadata', $amp_metadata
      ->get('id'));

    // Only delete the file if this is the only place it was in use.
    if (empty($this->fileUsage
      ->listUsage($logo_file_previous))) {
      $logo_file_previous
        ->setTemporary();
      $logo_file_previous
        ->delete();
    }
    $amp_metadata
      ->setOrganizationLogoFid(NULL);
  }
  $amp_metadata
    ->setOrganizationLogoImageStyleId($form_state
    ->getValue('amp_organization_logo_image_style_id'));

  // Save content settings.
  $amp_metadata
    ->setContentSchemaType($form_state
    ->getValue('amp_content_schema_type'));
  $amp_metadata
    ->setContentHeadlineToken($form_state
    ->getValue('amp_content_headline'));
  $amp_metadata
    ->setContentAuthorToken($form_state
    ->getValue('amp_content_author'));
  $amp_metadata
    ->setContentDescriptionToken($form_state
    ->getValue('amp_content_description'));
  $amp_metadata
    ->setContentImageToken($form_state
    ->getValue('amp_content_image'));
  $amp_metadata
    ->setContentImageStyleId($form_state
    ->getValue('amp_content_image_style_id'));

  // Save the metadata and set a message about saving the data.
  $status = $amp_metadata
    ->save();
  $action = $status == SAVED_UPDATED ? 'Updated' : 'Created';
  if ($amp_metadata
    ->isGlobal()) {
    drupal_set_message($this
      ->t('@action the global settings for  AMP Metadata.', [
      '@action' => $action,
    ]));
  }
  else {
    drupal_set_message($this
      ->t('@action the @type settings for AMP Metadata.', [
      '@action' => $action,
      '@type' => $this->entityTypeManager
        ->getStorage('node_type')
        ->load($amp_metadata
        ->getNodeType())
        ->label(),
    ]));
  }
  $form_state
    ->setRedirectUrl($amp_metadata
    ->urlInfo('collection'));
}