taxonomy_menu_sync-1.0.4/src/Entity/TaxonomyMenuSync.php

src/Entity/TaxonomyMenuSync.php
<?php

namespace Drupal\taxonomy_menu_sync\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Defines the taxonomy menu sync entity.
 *
 * @ContentEntityType(
 *   id = "taxonomy_menu_sync",
 *   label = @Translation("Taxonomy menu sync"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\taxonomy_menu_sync\TaxonomyMenuSyncListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *        "add" = "Drupal\taxonomy_menu_sync\Form\TaxonomyMenuSyncForm",
 *        "edit" = "Drupal\taxonomy_menu_sync\Form\TaxonomyMenuSyncForm",
 *        "delete" = "Drupal\taxonomy_menu_sync\Form\TaxonomyMenuSyncDeleteForm",
 *      },
 *      "access" = "Drupal\taxonomy_menu_sync\TaxonomyMenuSyncAccessControlHandler",
 *      "route_provider" = {
 *        "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
 *      },
 *    },
 *   translatable = FALSE,
 *   base_table = "taxonomy_menu_sync",
 *   admin_permission = "administer taxonomy menu sync entity",
 *   entity_keys = {
 *      "id" = "id",
 *      "label" = "title",
 *      "uuid" = "uuid",
 *      "source_vocabulary" = "source_vocabulary",
 *      "target_menu" = "target_menu",
 *      "source_bundle" = "source_bundle",
 *   },
 *   links = {
 *      "canonical" = "/admin/structure/taxonomy_menu_sync/{taxonomy_menu_sync}",
 *      "add-form" = "/admin/structure/taxonomy_menu_sync/add",
 *      "edit-form" = "/admin/structure/taxonomy_menu_sync/{taxonomy_menu_sync}/edit",
 *      "delete-form" = "/admin/structure/taxonomy_menu_sync/{taxonomy_menu_sync}/delete",
 *      "collection" = "/admin/structure/taxonomy_menu_sync"
 *   },
 *  )
 */
class TaxonomyMenuSync extends ContentEntityBase implements ContentEntityInterface {
  use EntityChangedTrait;

  /**
   * Get mappings.
   */
  public function getMapping():array {
    $mapping = [];
    $field_value = $this->get('mapping')->value;
    if (!empty($field_value) && !empty($decoded_mapping = json_decode($field_value, TRUE))) {
      $mapping = is_array($decoded_mapping) ? $decoded_mapping : [];
    }
    return $mapping;
  }

  /**
   * Check taxonomy menu sync is synced.
   */
  public function isSynced() {
    return $this->get('is_synced')->value;
  }

  /**
   * Get the updated time.
   */
  public function getUpdatedTime() {
    return $this->get('changed')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   *
   * Define the field properties here.
   *
   * Field name, type and size determine the table structure.
   *
   * In addition, we can define how the field and its content can be manipulated
   * in the GUI. The behaviour of the widgets used can be determined here.
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the taxonomy_menu_sync entity.'))
      ->setReadOnly(TRUE);

    $fields['uuid'] = BaseFieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the taxonomy_menu_sync entity.'))
      ->setReadOnly(TRUE);

    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setSettings([
        'max_length' => 255,
        'text_processing' => 0,
      ])
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
      ])
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
      ]);

    $fields['source_vocabulary'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Source taxonomy vocabulary'))
      ->setRequired(TRUE)
      ->setSetting('target_type', 'taxonomy_vocabulary')
      ->setDisplayOptions('form', [
        'type' => 'options_select',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
      ]);

    $fields['source_term'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Source taxonomy term'))
      ->setDescription(t('The term ID under which to generate the tree. Leave blank to generate the tree for the entire vocabulary.'))
      ->setSetting('target_type', 'taxonomy_term')
      ->setSetting('handler', 'default')
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => 60,
          'autocomplete_type' => 'tags',
          'placeholder' => '',
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
      ]);

    $fields['include_source_term'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Include source taxonomy term'))
      ->setDescription(t('Check to include source taxonomy term from the taxonomy tree.'))
      ->setDefaultValue(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['depth'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Depth'))
      ->setDescription(t('The number of levels of the tree to return. Leave blank to return all levels, and 0 to return only source taxonomy term.'))
      ->setSetting('min', 0)
      ->setSetting('max', 10)
      ->setSetting('step', 1)
      ->setDisplayOptions('form', [
        'type' => 'number',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'number_integer',
      ]);

    $fields['target_menu'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Target menu'))
      ->setRequired(TRUE)
      ->setSetting('target_type', 'menu')
      ->setDisplayOptions('form', [
        'type' => 'options_select',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
      ]);

    $fields['target_menu_item'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Target menu item'))
      ->setDescription(t('Taxonomy terms will be sync under this menu item.'))
      ->setSettings([
        'allowed_values_function' => [self::class, 'getMenuLinkContentList'],
      ])
      ->setDisplayOptions('form', [
        'type' => 'options_select',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'list_default',
      ]);

    $fields['hide_empty_term'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Disable menu item'))
      ->setDescription(t('Disable menu item if not associated with any configured content type.'))
      ->setDefaultValue(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['use_term_weight'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Use taxonomy term weight'))
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['source_bundle'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Content type'))
      ->setDescription(t('Node id of this content type will be used to generate menu links.'))
      ->setRequired(TRUE)
      ->setSetting('target_type', 'node_type')
      ->setDisplayOptions('form', [
        'type' => 'options_select',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
      ]);

    $fields['use_entity'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Use node data (i.e. label, URL)'))
      ->setDefaultValue(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['disable_title_sync'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Disable title sync '))
      ->setDescription(t('Disable title update of menu item on sync, if already created.'))
      ->setDefaultValue(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['disable_parent_sync'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Disable parent sync '))
      ->setDescription(t('Disable parent update of menu item on sync, if already created.'))
      ->setDefaultValue(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['is_synced'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Is synced'))
      ->setDescription(t('The time that the entity was last synced.'))
      ->setDefaultValue(FALSE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'boolean',
        'settings' => [
          'format' => 'yes-no',
        ],
      ]);

    $fields['mapping'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Term id v/s Menu item mapping'))
      ->setDescription(t('The mapping of term_id and menu_id.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'text_long',
      ]);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the entity was last edited.'));

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the entity was created.'));

    return $fields;
  }

  /**
   * Get menu content links (Parent selector).
   *
   * @return array
   */
  public static function getMenuLinkContentList() {
    /** @var \Drupal\taxonomy_menu_sync\TaxonomyMenuSyncHelper $helper */
    $helper = \Drupal::service('taxonomy_menu_sync.helper');
    return $helper->getMenuLinkContentList();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc