You are here

function _shrinktheweb_submit_handler in ShrinkTheWeb 8

Parameters

$form:

\Drupal\Core\Form\FormStateInterface $form_state:

1 string reference to '_shrinktheweb_submit_handler'
shrinktheweb_form_entity_view_display_edit_form_alter in ./shrinktheweb.module
Implements hook_form_alter().

File

./shrinktheweb.module, line 311

Code

function _shrinktheweb_submit_handler($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $fields = $form_state
    ->getValues();
  $shrinktheweb_fields = \Drupal::database()
    ->select('shrinktheweb_fields', 't')
    ->fields('t', array(
    'stw_field_name',
  ))
    ->condition('stw_entity_bundle', $form['#bundle'])
    ->execute()
    ->fetchCol();
  foreach ($fields['fields'] as $key => $value) {

    // Stw formatter was set for new field.
    if (strpos($value['type'], 'shrinktheweb') !== FALSE && !in_array($key, $shrinktheweb_fields)) {

      // Save new field id in database.
      $db_fields = array(
        'stw_field_name' => $key,
        'stw_entity_bundle' => $form['#bundle'],
      );
      \Drupal::database()
        ->merge('shrinktheweb_fields')
        ->key(array(
        'stw_field_name' => $key,
      ))
        ->fields($db_fields)
        ->updateFields($db_fields)
        ->execute();
    }

    // Stw formatter was unset for old field.
    if (strpos($value['type'], 'shrinktheweb') === FALSE && in_array($key, $shrinktheweb_fields)) {

      // Remove old field from database.
      \Drupal::database()
        ->delete('shrinktheweb_fields')
        ->condition('stw_field_name', $key, '=')
        ->execute();
    }
  }
}