You are here

class AudioFieldFieldFormatter in AudioField 8

Plugin implementation of audio player file formatter.

Plugin annotation


@FieldFormatter(
  id = "audiofield_audioplayer",
  label = @Translation("Audiofield Audio Player"),
  field_types = {
    "file", "link"
  }
)

Hierarchy

Expanded class hierarchy of AudioFieldFieldFormatter

File

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

Namespace

Drupal\audiofield\Plugin\Field\FieldFormatter
View source
class AudioFieldFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * Audio player management service.
   *
   * @var \Drupal\audiofield\AudioFieldPlayerManager
   */
  protected $audioPlayerManager;

  /**
   * {@inheritdoc}
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AudioFieldPlayerManager $audio_player_manager) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->audioPlayerManager = $audio_player_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('plugin.manager.audiofield'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $plugin_definitions = $this->audioPlayerManager
      ->getDefinitions();
    $settings = $this
      ->getSettings();

    // Show which player we are currently using for the field.
    $summary = [
      $this
        ->t('Selected player: <strong>@player</strong>', [
        '@player' => $plugin_definitions[$settings['audio_player']]['title'],
      ]),
    ];

    // If this is jPlayer, add those settings.
    if ($settings['audio_player'] == 'jplayer_audio_player') {

      // Display theme.
      $theme = 'None (for styling manually with CSS)';

      // If this is the custom jplayer circle theme.
      if ($settings['audio_player_jplayer_theme'] == 'audiofield.jplayer.theme_jplayer_circle') {
        $theme = 'jPlayer circle player';
      }
      else {
        foreach (_audiofield_list_skins('jplayer_audio_player') as $skin) {
          if ($skin['library_name'] == $settings['audio_player_jplayer_theme']) {
            $theme = $skin['name'];
          }
        }
      }
      $summary[] = $this
        ->t('Skin: <strong>@theme</strong>', [
        '@theme' => $theme,
      ]);
    }
    elseif ($settings['audio_player'] == 'wavesurfer_audio_player') {
      $summary[] = $this
        ->t('Combine files into single player? <strong>@combine</strong>', [
        '@combine' => $settings['audio_player_wavesurfer_combine_files'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Audio Rate: <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_audiorate'],
      ]);
      $summary[] = $this
        ->t('Auto Center? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_autocenter'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Autoplay next track? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_playnexttrack'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Bar Gap: <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_bargap'],
      ]);
      $summary[] = $this
        ->t('Bar Height: <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_barheight'],
      ]);
      $summary[] = $this
        ->t('Bar Width: <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_barwidth'],
      ]);
      $summary[] = $this
        ->t('Cursor Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
        '@value' => $settings['audio_player_wavesurfer_cursorcolor'],
      ]);
      $summary[] = $this
        ->t('Cursor Width: <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_cursorwidth'],
      ]);
      $summary[] = $this
        ->t('Force Decode? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_forcedecode'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Normalize? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_normalize'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Progress Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
        '@value' => $settings['audio_player_wavesurfer_progresscolor'],
      ]);
      $summary[] = $this
        ->t('Responsive? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_responsive'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Wave Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
        '@value' => $settings['audio_player_wavesurfer_wavecolor'],
      ]);
      $summary[] = $this
        ->t('Use Peak File? <strong>@value</strong>', [
        '@value' => $settings['audio_player_wavesurfer_use_peakfile'] ? 'Yes' : 'No',
      ]);
    }
    elseif ($settings['audio_player'] == 'wordpress_audio_player') {
      $summary[] = $this
        ->t('Combine files into single player? <strong>@combine</strong>', [
        '@combine' => $settings['audio_player_wordpress_combine_files'] ? 'Yes' : 'No',
      ]);
      $summary[] = $this
        ->t('Animate player? <strong>@animate</strong>', [
        '@animate' => $settings['audio_player_wordpress_animation'] ? 'Yes' : 'No',
      ]);
    }
    elseif ($settings['audio_player'] == 'soundmanager_audio_player') {
      $skins = [
        'default' => 'Default theme',
        'player360' => '360 degree player',
        'barui' => 'Bar UI',
        'inlineplayer' => 'Inline Player',
      ];
      $summary[] = $this
        ->t('Skin: <strong>@skin</strong>', [
        '@skin' => $skins[$settings['audio_player_soundmanager_theme']],
      ]);
    }

    // Show combined settings for multiple players.
    if (in_array($settings['audio_player'], [
      'jplayer_audio_player',
      'mediaelement_audio_player',
      'projekktor_audio_player',
      'soundmanager_audio_player',
      'wavesurfer_audio_player',
      'wordpress_audio_player',
    ])) {

      // Display volume.
      $summary[] = $this
        ->t('Initial volume: <strong>@volume out of 10</strong>', [
        '@volume' => $settings['audio_player_initial_volume'],
      ]);
    }

    // Display autoplay.
    if (in_array($settings['audio_player'], [
      'audiojs_audio_player',
      'default_mp3_player',
      'jplayer_audio_player',
      'mediaelement_audio_player',
      'projekktor_audio_player',
      'wavesurfer_audio_player',
      'wordpress_audio_player',
    ])) {
      $summary[] = $this
        ->t('Autoplay: <strong>@autoplay</strong>', [
        '@autoplay' => $settings['audio_player_autoplay'] ? 'Yes' : 'No',
      ]);
    }

    // Display lazy load.
    if (in_array($settings['audio_player'], [
      'audiojs_audio_player',
      'default_mp3_player',
      'jplayer_audio_player',
      'mediaelement_audio_player',
      'projekktor_audio_player',
    ])) {
      $summary[] = $this
        ->t('Lazy Load: <strong>@lazyload</strong>', [
        '@lazyload' => $settings['audio_player_lazyload'] ? 'Yes' : 'No',
      ]);
    }

    // Check to make sure the library is installed.
    $player = $this->audioPlayerManager
      ->createInstance($settings['audio_player']);
    if (!$player
      ->checkInstalled()) {
      $summary[] = $this
        ->t('<strong style="color:red;">Error: this player library is currently not installed. Please select another player or reinstall the library.</strong>');
    }

    // Show whether or not we are displaying download buttons.
    $summary[] = $this
      ->t('Display download button: <strong>@display_link</strong>', [
      '@display_link' => $settings['download_button'] ? 'Yes' : 'No',
    ]);

    // Show whether or not we are displaying direct downloads.
    $summary[] = $this
      ->t('Display download link: <strong>@display_link</strong>', [
      '@display_link' => $settings['download_link'] ? 'Yes' : 'No',
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'audio_player' => 'default_mp3_player',
      'audio_player_jplayer_theme' => 'none',
      'audio_player_wavesurfer_combine_files' => FALSE,
      'audio_player_wavesurfer_audiorate' => 1,
      'audio_player_wavesurfer_autocenter' => TRUE,
      'audio_player_wavesurfer_bargap' => 0,
      'audio_player_wavesurfer_barheight' => 1,
      'audio_player_wavesurfer_barwidth' => NULL,
      'audio_player_wavesurfer_cursorcolor' => '#333',
      'audio_player_wavesurfer_cursorwidth' => 1,
      'audio_player_wavesurfer_forcedecode' => FALSE,
      'audio_player_wavesurfer_normalize' => FALSE,
      'audio_player_wavesurfer_playnexttrack' => TRUE,
      'audio_player_wavesurfer_progresscolor' => '#555',
      'audio_player_wavesurfer_responsive' => FALSE,
      'audio_player_wavesurfer_wavecolor' => '#999',
      'audio_player_wavesurfer_use_peakfile' => FALSE,
      'audio_player_wordpress_combine_files' => FALSE,
      'audio_player_wordpress_animation' => TRUE,
      'audio_player_soundmanager_theme' => 'default',
      'audio_player_initial_volume' => 8,
      'audio_player_autoplay' => FALSE,
      'audio_player_lazyload' => FALSE,
      'download_button' => FALSE,
      'download_link' => FALSE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    // Early opt-out if the field is empty.
    if (count($items) <= 0) {
      return $elements;
    }
    $plugin_id = $this
      ->getSetting('audio_player');
    $player = $this->audioPlayerManager
      ->createInstance($plugin_id);
    $elements[] = $player
      ->renderPlayer($items, $langcode, $this
      ->getSettings());
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AudioFieldFieldFormatter::$audioPlayerManager protected property Audio player management service.
AudioFieldFieldFormatter::create public static function Creates an instance of the plugin. Overrides FormatterBase::create
AudioFieldFieldFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
AudioFieldFieldFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
AudioFieldFieldFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
AudioFieldFieldFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
AudioFieldFieldFormatter::__construct public function Constructs a FormatterBase object. Overrides FormatterBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
FormatterBase::prepareView public function Allows formatters to load information for field values being displayed. Overrides FormatterInterface::prepareView 2
FormatterBase::view public function Builds a renderable array for a fully themed field. Overrides FormatterInterface::view 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.