class ParagraphsBackgroundPlugin

Provides a background image feature plugin.

Plugin annotation


@ParagraphsBehavior(
  id = "background",
  label = @Translation("Background"),
  description = @Translation("Image to be used as background for the paragraph."),
  weight = 3
)

Hierarchy

Expanded class hierarchy of ParagraphsBackgroundPlugin

File

paragraphs_collection/modules/paragraphs_collection_demo/src/Plugin/paragraphs/Behavior/ParagraphsBackgroundPlugin.php, line 23

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior
View source
class ParagraphsBackgroundPlugin extends ParagraphsBehaviorBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $paragraphs_type = $form_state
      ->getFormObject()
      ->getEntity();
    if ($paragraphs_type
      ->isNew()) {
      return [];
    }
    $image_field_options = $this
      ->getFieldNameOptions($paragraphs_type, 'image');

    // Show Image select form only if this entity has at least one image field.
    if (count($image_field_options) > 0) {
      $form['background_image_field'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Background field'),
        '#description' => $this
          ->t('Image field to be used as background.'),
        '#options' => $image_field_options,
        '#empty_value' => '',
        '#default_value' => count($image_field_options) == 1 ? key($image_field_options) : $this->configuration['background_image_field'],
      ];
    }
    else {
      $form['message'] = [
        '#type' => 'container',
        '#markup' => $this
          ->t('No image field type available. Please add at least one in the <a href=":link">Manage fields</a> page.', [
          ':link' => Url::fromRoute("entity.{$paragraphs_type->getEntityType()->getBundleOf()}.field_ui_fields", [
            $paragraphs_type
              ->getEntityTypeId() => $paragraphs_type
              ->id(),
          ])
            ->toString(),
        ]),
        '#attributes' => [
          'class' => [
            'messages messages--error',
          ],
        ],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getValue('background_image_field')) {
      $form_state
        ->setErrorByName('message', $this
        ->t('The Background plugin cannot be enabled without an image field.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['background_image_field'] = $form_state
      ->getValue('background_image_field');
  }

  /**
   * {@inheritdoc}
   */
  public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
    $build['#attributes']['class'][] = 'paragraphs-behavior-background';
    $build['#attached']['library'][] = 'paragraphs_collection_demo/background';
    foreach (Element::children($build) as $field) {
      if ($field == $this->configuration['background_image_field']) {

        // Put the selected field into the background.
        $build[$field]['#attributes']['class'][] = 'paragraphs-behavior-background--image';
      }
      else {

        // Identify all other elements to put them on top of the background.
        $build[$field]['#attributes']['class'][] = 'paragraphs-behavior-background--element';
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'background_image_field' => '',
    ];
  }

}

Members

Name Modifiers Typesort descending Description Overrides
ParagraphsBackgroundPlugin::buildConfigurationForm public function Overrides ParagraphsBehaviorBase::buildConfigurationForm
ParagraphsBackgroundPlugin::validateConfigurationForm public function Overrides ParagraphsBehaviorBase::validateConfigurationForm
ParagraphsBackgroundPlugin::submitConfigurationForm public function Overrides ParagraphsBehaviorBase::submitConfigurationForm
ParagraphsBackgroundPlugin::view public function Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface::view
ParagraphsBackgroundPlugin::defaultConfiguration public function Overrides ParagraphsBehaviorBase::defaultConfiguration
ParagraphsBehaviorBase::__construct public function Constructs a ParagraphsBehaviorBase object. 6
ParagraphsBehaviorBase::create public static function 6
ParagraphsBehaviorBase::getConfiguration public function
ParagraphsBehaviorBase::setConfiguration public function
ParagraphsBehaviorBase::calculateDependencies public function
ParagraphsBehaviorBase::preprocess public function Adds a default set of helper variables for preprocessors and templates. Overrides ParagraphsBehaviorInterface::preprocess 1
ParagraphsBehaviorBase::isApplicable public static function Returns if the plugin can be used for the provided Paragraphs type. Overrides ParagraphsBehaviorInterface::isApplicable 1
ParagraphsBehaviorBase::settingsSummary public function Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsSummary 8
ParagraphsBehaviorBase::buildBehaviorForm public function Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorInterface::buildBehaviorForm 9
ParagraphsBehaviorBase::validateBehaviorForm public function Validates the behavior fields form. Overrides ParagraphsBehaviorInterface::validateBehaviorForm 1
ParagraphsBehaviorBase::submitBehaviorForm public function Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorInterface::submitBehaviorForm 3
ParagraphsBehaviorBase::getFieldNameOptions public function Returns list of field names for the given paragraph type and field type. Overrides ParagraphsBehaviorInterface::getFieldNameOptions
ParagraphsBehaviorBase::filterBehaviorFormSubmitValues protected function Removes default behavior form values before storing the user-set ones.
ParagraphsBehaviorBase::settingsIcon public function Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsIcon 1
ParagraphsBehaviorBase::$entityFieldManager protected property The entity field manager.