class ParagraphsDemoUserText

Provides a Paragraphs conversion plugin.

Plugin annotation


@ParagraphsConversion(
  id = "paragraphs_demo_user_to_text",
  label = @Translation("Convert to Text"),
  source_type = "user",
  target_types = {"text"},
  weight = 0
)

Hierarchy

Expanded class hierarchy of ParagraphsDemoUserText

File

paragraphs/modules/paragraphs_demo/src/Plugin/paragraphs/Conversion/ParagraphsDemoUserText.php, line 20

Namespace

Drupal\paragraphs_demo\Plugin\paragraphs\Conversion
View source
class ParagraphsDemoUserText extends ParagraphsConversionBase {

  /**
   * {@inheritdoc}
   */
  public function convert(array $settings, ParagraphInterface $original_paragraph, array $converted_paragraphs = NULL) {
    $username = "Empty user field";
    if ($original_paragraph
      ->get('field_user_demo')->entity) {
      $username = $original_paragraph
        ->get('field_user_demo')->entity
        ->label();
    }
    $converted_paragraphs = [
      [
        'type' => 'text',
        'field_text_demo' => [
          'value' => $username,
        ],
      ],
    ];
    if ($settings['multiple']) {
      $converted_paragraphs[] = [
        'type' => 'text',
        'field_text_demo' => [
          'value' => $username,
        ],
      ];
    }
    return $converted_paragraphs;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConversionForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $form = [];
    $form['multiple'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Multiple'),
      '#description' => $this
        ->t('If selected the conversion will return two paragraphs.'),
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphsConversionBase::buildConfigurationForm public function
ParagraphsConversionBase::calculateDependencies public function
ParagraphsConversionBase::create public static function
ParagraphsConversionBase::defaultConfiguration public function
ParagraphsConversionBase::getConfiguration public function
ParagraphsConversionBase::setConfiguration public function
ParagraphsConversionBase::submitConfigurationForm public function
ParagraphsConversionBase::supports public function Check if the current plugin supports conversion for a paragraph. Overrides ParagraphsConversionInterface::supports
ParagraphsConversionBase::validateConfigurationForm public function
ParagraphsConversionBase::validateConversionForm public function Validates the conversion fields form. Overrides ParagraphsConversionInterface::validateConversionForm
ParagraphsConversionBase::__construct public function Constructs a ParagraphsBehaviorBase object.
ParagraphsDemoUserText::buildConversionForm public function Builds a conversion form to add extra settings to the conversion. Overrides ParagraphsConversionBase::buildConversionForm
ParagraphsDemoUserText::convert public function Submit the values taken from the form to store the values. Overrides ParagraphsConversionBase::convert