You are here

public function AudioFieldFieldFormatter::settingsForm in AudioField 8

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/AudioFieldFieldFormatter.php, line 61

Class

AudioFieldFieldFormatter
Plugin implementation of audio player file formatter.

Namespace

Drupal\audiofield\Plugin\Field\FieldFormatter

Code

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

  // Get the fieldname in a format that works for all forms.
  $fieldname = $this->fieldDefinition
    ->getItemDefinition()
    ->getFieldDefinition()
    ->getName();

  // Loop over each plugin type and create an entry for it.
  $plugin_definitions = $this->audioPlayerManager
    ->getDefinitions();
  $plugins = [
    'available' => [],
    'unavailable' => [],
  ];
  foreach ($plugin_definitions as $plugin_id => $plugin) {

    // Create an instance of the player.
    $player = $this->audioPlayerManager
      ->createInstance($plugin_id);
    if ($player
      ->checkInstalled()) {
      $plugins['available'][$plugin_id] = $plugin['title'];
    }
    else {
      $plugins['unavailable'][$plugin_id] = $plugin['title'];
    }
  }
  ksort($plugins['available']);

  // Build settings form for display on the structure page.
  $elements = parent::settingsForm($form, $form_state);
  $default_player = $this
    ->getSetting('audio_player');
  if (isset($plugins['unavailable'][$default_player])) {
    $default_player = 'default_mp3_player';
  }

  // Let user select the audio player.
  $elements['audio_player'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select Player'),
    '#default_value' => $default_player,
    '#options' => $plugins['available'],
  ];
  if (count($plugins['unavailable']) > 0) {
    ksort($plugins['unavailable']);
    $elements['unavailable'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Disabled Players:'),
      '#open' => TRUE,
      '#disabled' => TRUE,
      [
        '#type' => '#container',
        '#attributes' => [],
        '#children' => implode('<br/>', $plugins['unavailable']),
      ],
    ];
  }

  // Settings for jPlayer.
  // Only show when jPlayer is the selected audio player.
  $jplayer_options = [
    'none' => 'None (for styling manually with CSS)',
    // Add the circle skin in (special non-standard custom skin for jPlayer).
    'audiofield.jplayer.theme_jplayer_circle' => 'jPlayer circle player',
  ];

  // Build the list of jPlayer available skins.
  foreach (_audiofield_list_skins('jplayer_audio_player') as $skin) {
    $jplayer_options[$skin['library_name']] = $skin['name'];
  }
  $elements['audio_player_jplayer_theme'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Select jPlayer Skin'),
    '#description' => $this
      ->t('jPlayer comes bundled with multiple skins by default. You can install additional skins by placing them in /libraries/jplayer/dist/skin/'),
    '#default_value' => $this
      ->getSetting('audio_player_jplayer_theme'),
    '#options' => $jplayer_options,
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
          'value' => 'jplayer_audio_player',
        ],
      ],
    ],
  ];

  // Settings for WaveSurfer.
  // Only show when WaveSurfer is the selected audio player.
  $elements['audio_player_wavesurfer_combine_files'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Combine audio files into a single audio player'),
    '#description' => $this
      ->t('By default Wavesurfer displays files individually. This option combines the files into a playlist so only one file shows at a time.'),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_combine_files'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
          'value' => 'wavesurfer_audio_player',
        ],
      ],
    ],
  ];

  // Settings for WaveSurfer.
  $elements['audio_player_wavesurfer_audiorate'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Audio Rate'),
    '#description' => $this
      ->t("Speed at which to play audio. Lower number is slower."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_audiorate'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_autocenter'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Auto Center'),
    '#description' => $this
      ->t("If a scrollbar is present, center the waveform around the progress."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_autocenter'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_bargap'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Bar Gap'),
    '#description' => $this
      ->t("The optional spacing between bars of the wave."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_bargap'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_barheight'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Bar Height'),
    '#description' => $this
      ->t("Height of the waveform bars. Higher number than 1 will increase the waveform bar heights."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_barheight'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_barwidth'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Bar Width'),
    '#description' => $this
      ->t("If specified, the waveform will be drawn like this: ▁ ▂ ▇ ▃ ▅ ▂"),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_barwidth'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_cursorcolor'] = [
    '#type' => 'color',
    '#title' => $this
      ->t('Cursor Color'),
    '#description' => $this
      ->t("The fill color of the cursor indicating the playhead position."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_cursorcolor'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_cursorwidth'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Cursor Width'),
    '#description' => $this
      ->t("Width of the cursor indicating the playhead position. Measured in pixels."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_cursorwidth'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_forcedecode'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force Decode'),
    '#description' => $this
      ->t("Force decoding of audio using web audio when zooming to get a more detailed waveform."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_forcedecode'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_normalize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Normalize'),
    '#description' => $this
      ->t("If checked, normalize by the maximum peak instead of 1.0."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_normalize'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_progresscolor'] = [
    '#type' => 'color',
    '#title' => $this
      ->t('Progress Color'),
    '#description' => $this
      ->t("The fill color of the part of the waveform behind the cursor."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_progresscolor'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_responsive'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Responsive'),
    '#description' => $this
      ->t("If checked, resize the waveform, when the window is resized. This is debounced with a 100ms timeout by default."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_responsive'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_wavecolor'] = [
    '#type' => 'color',
    '#title' => $this
      ->t('Wave Color'),
    '#description' => $this
      ->t("The fill color of the waveform after the cursor."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_wavecolor'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_use_peakfile'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use Peak File'),
    '#description' => $this
      ->t("Peak files are used to speed up waveform display on the client and reduce the load on the server by pre-rendering the waveform. These are stored alongside your audio files and are automatically generated."),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_use_peakfile'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];
  $elements['audio_player_wavesurfer_playnexttrack'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Automatically skip to next track'),
    '#description' => $this
      ->t("If checked, next track in playlist will auto-play"),
    '#default_value' => $this
      ->getSetting('audio_player_wavesurfer_playnexttrack'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
      ],
    ],
  ];

  // Settings for WordPress.
  // Only show when WordPress is the selected audio player.
  $elements['audio_player_wordpress_combine_files'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Combine audio files into a single audio player'),
    '#description' => $this
      ->t('This can be more difficult to see for the WordPress plugin. Multiple files are represented only by small "next" and "previous" arrows. Unchecking this box causes each file to be rendered as its own player.'),
    '#default_value' => $this
      ->getSetting('audio_player_wordpress_combine_files'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
          'value' => 'wordpress_audio_player',
        ],
      ],
    ],
  ];
  $elements['audio_player_wordpress_animation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Animate player?'),
    '#description' => $this
      ->t('If unchecked, the player will always remain open with the title visible.'),
    '#default_value' => $this
      ->getSetting('audio_player_wordpress_animation'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
          'value' => 'wordpress_audio_player',
        ],
      ],
    ],
  ];

  // Settings for SoundManager.
  // Only show when SoundManager is the selected audio player.
  $elements['audio_player_soundmanager_theme'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Select SoundManager Skin'),
    '#default_value' => $this
      ->getSetting('audio_player_soundmanager_theme'),
    '#options' => [
      'default' => $this
        ->t('Default theme'),
      'player360' => $this
        ->t('360 degree player'),
      'barui' => $this
        ->t('Bar UI'),
      'inlineplayer' => $this
        ->t('Inline Player'),
    ],
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
          'value' => 'soundmanager_audio_player',
        ],
      ],
    ],
  ];

  // Settings for multiple players.
  $elements['audio_player_initial_volume'] = [
    '#type' => 'range',
    '#title' => $this
      ->t('Set Initial Volume'),
    '#default_value' => $this
      ->getSetting('audio_player_initial_volume'),
    '#min' => 0,
    '#max' => 10,
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'jplayer_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'mediaelement_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'projekktor_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'soundmanager_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wordpress_audio_player',
          ],
        ],
      ],
    ],
  ];

  // Settings for autoplay.
  $elements['audio_player_autoplay'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Autoplay on page load'),
    '#default_value' => $this
      ->getSetting('audio_player_autoplay'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'audiojs_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'default_mp3_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'jplayer_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'mediaelement_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'projekktor_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wavesurfer_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'wordpress_audio_player',
          ],
        ],
      ],
    ],
  ];

  // Settings for autoplay.
  $elements['audio_player_lazyload'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Lazy Load audio'),
    '#description' => $this
      ->t("This setting causes audio not to be loaded until it is played."),
    '#default_value' => $this
      ->getSetting('audio_player_lazyload'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'audiojs_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'default_mp3_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'jplayer_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'mediaelement_audio_player',
          ],
        ],
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'projekktor_audio_player',
          ],
        ],
      ],
    ],
  ];

  // Settings for download button.
  $elements['download_button'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display download button on player'),
    '#default_value' => $this
      ->getSetting('download_button'),
    '#states' => [
      'visible' => [
        [
          ':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => [
            'value' => 'default_mp3_player',
          ],
        ],
      ],
    ],
  ];

  // Setting for optional download link.
  $elements['download_link'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display download link below player'),
    '#default_value' => $this
      ->getSetting('download_link'),
  ];
  return $elements;
}