composer.json000064400000000360150126057430007270 0ustar00{ "name": "4i4/tickets", "description": "Tickets configurations.", "type": "drupal-module", "authors": [ { "name": "Petyo Stoyanov", "email": "pss@4i4.io" } ], "require": {} } config/schema/tickets.schema.yml000064400000000425150126057430012705 0ustar00tickets.ticket_type.*: type: config_entity label: 'Ticket type' mapping: id: type: string label: 'ID' label: type: label label: 'Label' bundle: type: string label: 'Bundle' group: type: label label: 'Group' src/Entity/TicketType.php000064400000003771150126057430011420 0ustar00bundle; } public function setBundle($bundle) { $this->bundle = $bundle; } public function getGroup() { return $this->group; } public function setGroup($group) { $this->group = $group; } } src/Form/TicketTypeDeleteForm.php000064400000001723150126057430013011 0ustar00t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); } /** * {@inheritdoc} */ public function getCancelUrl() { return new Url('entity.ticket_type.collection'); } /** * {@inheritdoc} */ public function getConfirmText() { return $this->t('Delete'); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->entity->delete(); $this->messenger()->addMessage($this->t('Entity %label has been deleted.', ['%label' => $this->entity->label()])); $form_state->setRedirectUrl($this->getCancelUrl()); } } src/Form/TicketTypeForm.php000064400000005223150126057430011665 0ustar00entityTypeManager = $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); /** @var \Drupal\tickets\TicketTypeInterface $type */ $type = $this->entity; $form['label'] = [ '#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $type->label(), '#description' => $this->t("Label for the Example."), '#required' => TRUE, ]; $form['id'] = [ '#type' => 'machine_name', '#default_value' => $type->id(), '#machine_name' => [ 'exists' => [$this, 'exist'], ], '#disabled' => !$type->isNew(), ]; $form['bundle'] = [ '#type' => 'checkbox', '#title' => $this->t('Bundle'), '#default_value' => $type->getBundle(), ]; $form['group'] = [ '#type' => 'checkbox', '#title' => $this->t('Group'), '#default_value' => $type->getGroup(), ]; return $form; } /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { /** @var \Drupal\tickets\TicketTypeInterface $type */ $type = $this->entity; $status = $type->save(); if ($status === SAVED_NEW) { $this->messenger()->addMessage($this->t('The %label Ticket type created.', [ '%label' => $type->label(), ])); } else { $this->messenger()->addMessage($this->t('The %label Ticket type updated.', [ '%label' => $type->label(), ])); } $form_state->setRedirect('entity.ticket_type.collection'); } /** * Helper function to check whether an Ticket type configuration entity exists. */ public function exist($id) { $entity = $this->entityTypeManager->getStorage('ticket_type')->getQuery() ->condition('id', $id) ->execute(); return (bool) $entity; } } src/Plugin/Field/FieldFormatter/TicketFormatter.php000064400000001362150126057430016350 0ustar00 $item) { $output = $item->value; $elements[$delta] = ['#markup' => $output]; } return $elements; } } src/Plugin/Field/FieldType/TicketItem.php000064400000005435150126057430014266 0ustar00 [ 'min' => [ 'type' => 'int', 'unsigned' => FALSE, 'size' => "normal", 'not null' => FALSE, ], 'max' => [ 'type' => 'int', 'unsigned' => FALSE, 'size' => "normal", 'not null' => FALSE, ], 'per' => [ 'type' => 'varchar', 'length' => 256, 'not null' => FALSE, ], 'type' => [ 'type' => 'varchar', 'length' => 256, ], 'quantity' => [ 'type' => 'int', 'unsigned' => FALSE, 'size' => "normal", ], 'value' => [ 'type' => 'numeric', 'precision' => 10, 'scale' => 2, ], ], ]; } /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties = []; $properties['per'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('Per')) ->addConstraint('Length', ['max' => 255]) ->setRequired(FALSE); $properties['type'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('Type')) ->addConstraint('Length', ['max' => 255]) ->setRequired(TRUE); $properties['quantity'] = DataDefinition::create('integer') ->setLabel(new TranslatableMarkup('Quantity value')) ->setRequired(FALSE); $properties['min'] = DataDefinition::create('integer') ->setLabel(new TranslatableMarkup('Min')) ->setRequired(FALSE); $properties['max'] = DataDefinition::create('integer') ->setLabel(new TranslatableMarkup('Max')) ->setRequired(FALSE); $properties['value'] = DataDefinition::create('decimal') ->setLabel(new TranslatableMarkup('Price value')) ->setRequired(TRUE); return $properties; } /** * {@inheritdoc} */ public function isEmpty() { $type = $this->get('type')->getValue(); $value = $this->get('value')->getValue(); return ($value === NULL || $value === '') && ($type === NULL || $type === ''); } /** * {@inheritdoc} */ public function preSave() { $this->value = round($this->value, 2); } } src/Plugin/Field/FieldWidget/TicketWidget.php000064400000007655150126057430015123 0ustar00storage = $container->get('entity_type.manager')->getStorage('ticket_type'); return $instance; } /** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $value = $items[$delta]->value ?? NULL; $type = $items[$delta]->type ?? NULL; $quantity = $items[$delta]->quantity ?? 1; $min = $items[$delta]->min ?? NULL; $max = $items[$delta]->max ?? NULL; $field_name = $this->fieldDefinition->getName(); $types = $this->storage->loadMultiple(); $options = ["" => t('- None -')]; $bundles = []; $groups = []; foreach ($types as $ticket_type) { $options[$ticket_type->id()] = $ticket_type->label(); if ($ticket_type->getBundle()) { $bundles[] = ['value' => $ticket_type->id()]; } if ($ticket_type->getGroup()) { $groups[] = ['value' => $ticket_type->id()]; } } $element += [ '#type' => 'container', '#id' => $field_name . "_" . $delta, '#attributes' => [ 'class' => ['container-inline'], ], ]; $element['type'] = [ '#type' => 'select', '#default_value' => $type, '#options' => $options, ]; $element['min'] = [ '#type' => 'number', '#default_value' => $min ?? 1, '#min' => 1, '#size' => 2, '#states' => [ 'visible' => [ 'select[name="' . $field_name . '[' . $delta .'][type]"]' => $groups, ] ] ]; $element['max'] = [ '#type' => 'number', '#default_value' => $max ?? 1, '#min' => 1, '#size' => 2, '#states' => [ 'visible' => [ 'select[name="' . $field_name . '[' . $delta .'][type]"]' => $groups, ] ] ]; $element['quantity'] = [ '#type' => 'number', '#default_value' => $quantity ?? 1, '#min' => 1, '#size' => 3, '#states' => [ 'visible' => [ 'select[name="' . $field_name . '[' . $delta .'][type]"]' => $bundles, ] ] ]; $element['value'] = [ '#type' => 'number', '#default_value' => $value, '#size' => 10, ]; $element['per'] = [ '#type' => 'select', '#default_value' => $type, '#options' => [ '' => t('- None -'), 'person' => $this->t('Person'), 'group' => $this->t('Group'), ], '#states' => [ 'visible' => [ 'select[name="' . $field_name . '[' . $delta .'][type]"]' => $groups, ] ] ]; return $element; } } src/TicketTypeInterface.php000064400000001456150126057430011763 0ustar00t('Example'); $header['id'] = $this->t('Machine name'); return $header + parent::buildHeader(); } /** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { $row['label'] = $entity->label(); $row['id'] = $entity->id(); // You probably want a few more properties here... return $row + parent::buildRow($entity); } } tickets.info.yml000064400000000162150126057430007671 0ustar00name: 'Tickets' type: 'module' description: 'Custom functions' package: 4i4 core_version_requirement: '^10 | ^11' tickets.links.action.yml000064400000000226150126057430011333 0ustar00entity.ticket_type.add_form: route_name: 'entity.ticket_type.add_form' title: 'Add ticket type' appears_on: - entity.ticket_type.collection tickets.links.menu.yml000064400000000254150126057430011023 0ustar00entity.ticket_type.collection: title: 'Ticket types' parent: system.admin_structure description: 'Configure ticket types' route_name: entity.ticket_type.collection tickets.routing.yml000064400000002111150126057430010421 0ustar00tickets.collection: path: '/admin/structure/tickets/types' defaults: _entity_list: 'ticket_type' _title: 'Ticket types configuration' requirements: _permission: 'administer ticket types' entity.ticket_type.collection: path: '/admin/structure/tickets/types' defaults: _entity_list: 'ticket_type' _title: 'Ticket types configuration' requirements: _permission: 'administer ticket types' entity.ticket_type.add_form: path: '/admin/structure/tickets/types/add' defaults: _entity_form: 'ticket_type.add' _title: 'Add ticket type' requirements: _permission: 'administer ticket types' entity.ticket_type.edit_form: path: '/admin/structure/tickets/types/{ticket_type}' defaults: _entity_form: 'ticket_type.edit' _title: 'Edit ticket type' requirements: _permission: 'administer ticket types' entity.ticket_type.delete_form: path: '/admin/structure/tickets/types/{ticket_type}/delete' defaults: _entity_form: 'ticket_type.delete' _title: 'Delete ticket type' requirements: _permission: 'administer ticket types'