You are here

public static function ContentBuilder::processBlockForm in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::processBlockForm()
  2. 8.8 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::processBlockForm()
  3. 10.0.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::processBlockForm()
  4. 10.1.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::processBlockForm()
  5. 10.2.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::processBlockForm()

Process callback to insert a Custom Block form.

Parameters

array $element: The containing element.

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

Return value

array The containing element, with the Custom Block form inserted.

File

modules/social_features/social_content_block/src/ContentBuilder.php, line 307

Class

ContentBuilder
Class ContentBuilder.

Namespace

Drupal\social_content_block

Code

public static function processBlockForm(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\social_content_block\ContentBlockManagerInterface $content_block_manager */
  $content_block_manager = \Drupal::service('plugin.manager.content_block');
  $selector = $content_block_manager
    ->getSelector('field_plugin_id', 'value');
  foreach ($content_block_manager
    ->getDefinitions() as $plugin_id => $plugin_definition) {
    $fields =& $element['field_plugin_field']['widget'][0][$plugin_id]['#options'];
    foreach ($fields as $field_name => $field_title) {

      // When the filter field was absent during executing the code of the
      // field widget plugin for the filters list field then process this
      // field repeatedly.
      // @see \Drupal\social_content_block\Plugin\Field\FieldWidget\ContentBlockPluginFieldWidget::formElement()
      if ($field_name === $field_title) {
        if (isset($element[$field_name]['widget']['target_id'])) {
          $fields[$field_name] = $element[$field_name]['widget']['target_id']['#title'];
        }
        else {
          $fields[$field_name] = $element[$field_name]['widget']['#title'];
        }
        $element[$field_name]['#states'] = [
          'visible' => [
            $selector => [
              'value' => $plugin_id,
            ],
            $content_block_manager
              ->getSelector('field_plugin_field', $plugin_id) => [
              [
                'value' => 'all',
              ],
              [
                'value' => $field_name,
              ],
            ],
          ],
        ];
      }
    }
  }

  // Add a callback to update sorting options based on the selected plugins.
  $element['field_plugin_id']['widget'][0]['value']['#ajax'] = [
    'callback' => [
      self::class,
      'updateFormSortingOptions',
    ],
    'wrapper' => 'social-content-block-sorting-options',
  ];
  $parents = array_merge($element['field_plugin_id']['widget']['#field_parents'], [
    'field_plugin_id',
  ]);

  // Set the sorting options based on the selected plugins.
  $value_parents = array_merge($parents, [
    '0',
    'value',
  ]);
  $selected_plugin = $form_state
    ->getValue($value_parents);

  // If there's no value in the form state check if there was anything in the
  // submissions.
  if ($selected_plugin === NULL) {
    $input = $form_state
      ->getUserInput();
    $field = $element['field_plugin_id']['widget'][0]['value'];
    if (NestedArray::keyExists($input, $value_parents)) {
      $input_value = NestedArray::getValue($input, $value_parents);
      if (!empty($input_value) && isset($field['#options'][$input_value])) {
        $selected_plugin = $input_value;
      }
    }

    // If nothing valid was selected yet then we fallback to the default.
    if (empty($selected_plugin)) {
      $selected_plugin = $field['#default_value'];
    }
  }
  $element['field_sorting']['widget']['#options'] = $content_block_manager
    ->createInstance($selected_plugin)
    ->supportedSortOptions();
  $element['field_sorting']['#prefix'] = '<div id="social-content-block-sorting-options">';
  $element['field_sorting']['#suffix'] = '</div>';
  return $element;
}