You are here

TagFormCopy.php in Extensible BBCode 8.3

Same filename and directory in other branches
  1. 4.0.x src/Form/TagFormCopy.php

File

src/Form/TagFormCopy.php
View source
<?php

namespace Drupal\xbbcode\Form;

use Drupal\Core\Entity\EntityInterface;

/**
 * Form for creating a copy of a BBCode tag.
 */
class TagFormCopy extends TagForm {

  /**
   * {@inheritdoc}
   */
  public function setEntity(EntityInterface $entity) {

    // Relabel the entity with a sequential number.
    $label = $entity
      ->label();
    if (preg_match('/^(.*?)\\s*(\\d+)$/', $label, $match)) {
      [
        ,
        $label,
        $number,
      ] = $match;
    }
    else {
      $number = 1;
    }

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $copy */
    $copy = $entity
      ->createDuplicate();
    $copy
      ->set('label', $label . ' ' . ($number + 1));
    parent::setEntity($copy);
  }

}

Classes

Namesort descending Description
TagFormCopy Form for creating a copy of a BBCode tag.