You are here

function fieldblock_field_display_submit in Field as Block 8

Same name and namespace in other branches
  1. 7 fieldblock.module \fieldblock_field_display_submit()

Form submit handler for field_ui_display_overview_form. Stores which fields are published as blocks as a third_party_settings array in the EntityViewDisplay object of the entity type / bundle / view mode.

Parameters

mixed[] $form: A form API array.

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

1 string reference to 'fieldblock_field_display_submit'
fieldblock_form_field_ui_display_overview_form_alter in ./fieldblock.module
Implements hook_form_alter().

File

./fieldblock.module, line 53
Allow fields to be rendered in blocks.

Code

function fieldblock_field_display_submit($form, FormStateInterface $form_state) {
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $mode = $form['#mode'];

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $entity_view_display */
  $entity_view_display = EntityViewDisplay::load($entity_type . '.' . $bundle . '.' . $mode);
  $fields = $form_state
    ->getValue('fields');
  foreach ($fields as $field_name => $field) {
    if (isset($field['fieldblock']) && $field['fieldblock'] == 1) {
      $entity_view_display
        ->setThirdPartySetting('fieldblock', $field_name, $form['fields'][$field_name]['human_name']['#markup']);
    }
    else {
      if ($entity_view_display
        ->getThirdPartySetting('fieldblock', $field_name)) {
        $entity_view_display
          ->unsetThirdPartySetting('fieldblock', $field_name);
      }
    }
  }
  $entity_view_display
    ->save();

  // Invalidate the block cache to update fielblock derivatives.
  if (\Drupal::moduleHandler()
    ->moduleExists('block')) {
    \Drupal::service('plugin.manager.block')
      ->clearCachedDefinitions();
  }
}