logo

Dodatni tipovi blokova (EBT) - Novo iskustvo rada sa Layout Builder-om❗

Dodatni tipovi blokova (EBT) – stilizovani, prilagodljivi tipovi blokova: slajdšouvi, kartice sa tabovima, kartice, akordeoni i mnogi drugi. Ugrađena podešavanja za pozadinu, DOM Box, javascript dodatke. Iskusite budućnost kreiranja rasporeda već danas.

Demo EBT moduli Preuzmite EBT module

❗Dodatni tipovi pasusa (EPT) – Novo iskustvo rada sa pasusima

Dodatni tipovi pasusa (EPT) – analogni skup modula zasnovan na pasusima.

Demo EPT moduli Preuzmite EPT module

Scroll

Kreiranje tipa konfiguracionog objekta u Drupalu 8

19/06/2025, by Ivan

Na ovoj stranici je prikazan primer kako kreirati tip konfiguracionog objekta sa stranicama za upravljanje administracijom u Drupalu 8. Za upoznavanje sa pojmovima jednostavne konfiguracije i konfiguracionih objekata pogledajte Https://drupal.org/node/2120523.

Nakon što omogućite primer modula koji sadrži donji kod, primer konfiguracione forme bi trebalo da bude dostupan na adresi „admin/config/system/example“, kao što je prikazano na slici:

2016-12-18-002716

Podešavanje modula i ulazak u administratorski meni

example/example.info.yml

name: Example
description: 'Upravljanje primer konfiguracijom.'
package: Example

type: module
core: 8.x

Ruting

(Pogledajte neke pomoćne klase dodate za rad sa putanjama entiteta da biste saznali kako to pojednostaviti.)

example/example.routing.yml

Fajl routing.yml definiše rute za administrativne stranice: listu, dodavanje, izmenu, brisanje.

entity.example.collection:
  path: '/admin/config/system/example'
  defaults:
    _entity_list: 'example'
    _title: 'Primer konfiguracije'
  requirements:
    _permission: 'administer site configuration'

entity.example.add_form:
  path: '/admin/config/system/example/add'
  defaults:
    _entity_form: 'example.add'
    _title: 'Dodaj primer'
  requirements:
    _permission: 'administer site configuration'

entity.example.edit_form:
  path: '/admin/config/system/example/{example}'
  defaults:
    _entity_form: 'example.edit'
    _title: 'Izmeni primer'
  requirements:
    _permission: 'administer site configuration'

entity.example.delete_form:
  path: '/admin/config/system/example/{example}/delete'
  defaults:
    _entity_form: 'example.delete'
    _title: 'Obriši primer'
  requirements:
    _permission: 'administer site configuration'

example/example.links.menu.yml

Ovo dodaje link do stranice konfiguracije u Sistem

entity.example.collection:
  title: 'Primer'
  parent: system.admin_config_system
  description: 'Podesi primer'
  route_name: entity.example.collection

example/example.links.action.yml

Ovim se omogućava da link „Dodaj“ bude prikazan na stranici liste.

entity.example.add_form:
  route_name: 'entity.example.add_form'
  title: 'Dodaj primer'
  appears_on:
    - entity.example.collection

Klasе tipova objekata

example/src/ExampleInterface.php

Pod pretpostavkom da vaš konfiguracioni entitet ima svojstva, trebaće vam da definišete metode set/get u interfejsu.
 

namespace Drupal\example;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
 * Omogućava interfejs koji definiše Example entitet.
 */
interface ExampleInterface extends ConfigEntityInterface {
  // Dodajte metode za dobijanje i postavljanje vaših konfiguracionih svojstava ovde.
}

example/src/Entity/Example.php

Ovaj fajl definiše klasu konfiguracionog entiteta.
 

namespace Drupal\example\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\example\ExampleInterface;

/**
 * Definiše Example entitet.
 *
 * @ConfigEntityType(
 *   id = "example",
 *   label = @Translation("Primer"),
 *   handlers = {
 *     "list_builder" = "Drupal\example\Controller\ExampleListBuilder",
 *     "form" = {
 *       "add" = "Drupal\example\Form\ExampleForm",
 *       "edit" = "Drupal\example\Form\ExampleForm",
 *       "delete" = "Drupal\example\Form\ExampleDeleteForm",
 *     }
 *   },
 *   config_prefix = "example",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *   },
 *   config_export = {
 *     "id",
 *     "label"
 *   },
 *   links = {
 *     "edit-form" = "/admin/config/system/example/{example}",
 *     "delete-form" = "/admin/config/system/example/{example}/delete",
 *   }
 * )
 */
class Example extends ConfigEntityBase implements ExampleInterface {

  /**
   * ID primera.
   *
   * @var string
   */
  public $id;

  /**
   * Oznaka primera.
   *
   * @var string
   */
  public $label;

  // Vaše specifične metode za dobijanje/postavljanje svojstava,
  // implementirajući interfejs.
}

Ključ admin_permission automatski dozvoljava pristup svim korisnicima sa tim dozvolama. Ako vam je potrebna složenija logika, možete definisati prilagođeni kontroler pristupa.

Od Drupal 8.6.x se preporučuje da svi tipovi konfiguracionih objekata imaju svojstvo config_export u svojim anotacijama (videti: https://www.drupal.org/node/2949023).

Fajl šeme konfiguracije

example/config/schema/example.schema.yml

example.example.*:
  type: config_entity
  label: 'Primer konfiguracije'
  mapping:
    id:
      type: string
      label: 'ID'
    label:
      type: label
      label: 'Oznaka'

U example.schema.yml dodajte svojstva/atribute definisane u \Drupal\example\Entity\Example

example.example.* je konfiguraciona promenljiva koja se odnosi na svojstva/atribute naše klase, i možete koristiti drugo ime promenljive za vaš entitet tako što ćete dodati „config_prefix“, na primer:

@ConfigEntityType(
..
... config_prefix = "variable_name" ... 

onda možete referencirati ovo na sledeći način:

example.variable_name.*: ....

Za više informacija o konfiguracionoj šemi pogledajte Konfiguraciona šema/metadata

Klasе entiteta

example/src/Form/ExampleForm.php

namespace Drupal\example\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Rukovalac forme za dodavanje i izmenu primera.
 */
class ExampleForm extends EntityForm {

  /**
   * Konstruktor za ExampleForm objekat.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Menadžer tipa entiteta.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    $example = $this->entity;

    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Oznaka'),
      '#maxlength' => 255,
      '#default_value' => $example->label(),
      '#description' => $this->t("Oznaka za primer."),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $example->id(),
      '#machine_name' => [
        'exists' => [$this, 'exist'],
      ],
      '#disabled' => !$example->isNew(),
    ];

    // Trebaće vam dodatni elementi forme za vaše prilagođene osobine.
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $example = $this->entity;
    $status = $example->save();

    if ($status === SAVED_NEW) {
      $this->messenger()->addMessage($this->t('Primer %label je kreiran.', [
        '%label' => $example->label(),
      ]));
    }
    else {
      $this->messenger()->addMessage($this->t('Primer %label je izmenjen.', [
        '%label' => $example->label(),
      ]));
    }

    $form_state->setRedirect('entity.example.collection');
  }

  /**
   * Pomoćna funkcija za proveru da li primer konfiguracionog entiteta postoji.
   */
  public function exist($id) {
    $entity = $this->entityTypeManager->getStorage('example')->getQuery()
      ->condition('id', $id)
      ->execute();
    return (bool) $entity;
  }

}

example/src/Controller/ExampleListBuilder.php

namespace Drupal\example\Controller;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;

/**
 * Omogućava listu primera.
 */
class ExampleListBuilder extends ConfigEntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this->t('Primer');
    $header['id'] = $this->t('Mašinsko ime');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row['label'] = $entity->label();
    $row['id'] = $entity->id();

    // Verovatno ćete želeti još svojstava ovde...

    return $row + parent::buildRow($entity);
  }

}

example/src/Form/ExampleDeleteForm.php
 

namespace Drupal\example\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Pravi formu za brisanje primera.
 */

class ExampleDeleteForm extends EntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Da li ste sigurni da želite da obrišete %name?', array('%name' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.example.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Obriši');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity->delete();
    $this->messenger()->addMessage($this->t('Entitet %label je obrisan.', array('%label' => $this->entity->label())));

    $form_state->setRedirectUrl($this->getCancelUrl());
  }
}

 

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.