<?php

/**
 * @file
 * File containing d_p_form module.
 */

use Drupal\d_p\Exception\MissingConfigurationStorageFieldException;
use Drupal\d_p\ParagraphSettingTypesInterface;
use Drupal\d_p\Plugin\Field\FieldType\ConfigurationStorage;

/**
 * Implements hook_options_list_alter().
 */
function d_p_form_options_list_alter(array &$options, array $context) {
  // Personal form makes error.
  if (method_exists($context['fieldDefinition'], 'id')) {
    if ($context['fieldDefinition']->id() == 'paragraph.d_p_form.field_d_forms') {
      unset($options['personal']);
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_p_form_preprocess_paragraph__d_p_form(array &$variables) {
  $paragraph = $variables['paragraph'];
  try {
    /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */
    $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph);

    $variables['d_p_form_placement'] = $setting_field
      ->getSettingValue(ParagraphSettingTypesInterface::PARAGRAPH_SETTING_FORM_LAYOUT);
  }
  catch (MissingConfigurationStorageFieldException $exception) {
    \Drupal::logger('d_p_form')->error($exception->getMessage());
  }
}

/**
 * Implements hook_theme().
 */
function d_p_form_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__d_p_form' => [
      'base hook' => 'paragraph',
    ],
  ];
}
