Commit 178febc0 authored by Tavi Toporjinschi's avatar Tavi Toporjinschi
Browse files

Issue #2464805 by bojanz, googletorp, vasike: Complete the store data model -...

Issue #2464805 by bojanz, googletorp, vasike: Complete the store data model - currencies, default store config, fields ordering.
parent 863d1428
Loading
Loading
Loading
Loading
+19 −4
Changes for modules/store/src/Entity/Store.php: 19 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ class Store extends ContentEntityBase implements StoreInterface {
      ->setSetting('target_type', 'user')
      ->setDisplayOptions('form', array(
        'type' => 'entity_reference_autocomplete',
        'weight' => 0,
        'weight' => 50,
      ));

    $fields['langcode'] = BaseFieldDefinition::create('language')
@@ -196,7 +196,7 @@ class Store extends ContentEntityBase implements StoreInterface {
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string_textfield',
        'weight' => 0,
        'weight' => -10,
      ));

    $fields['type'] = BaseFieldDefinition::create('string')
@@ -213,7 +213,7 @@ class Store extends ContentEntityBase implements StoreInterface {
        'weight' => 0,
      ));

    $fields['default_currency'] = BaseFieldDefinition::create('string')
    $fields['default_currency'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Default currency'))
      ->setDescription(t('The default currency of this store.'))
      ->setCardinality(1)
@@ -224,7 +224,22 @@ class Store extends ContentEntityBase implements StoreInterface {
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'options_select',
        'weight' => 0,
        'weight' => 10,
        'settings' => array(),
      ));

    $fields['currencies'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Currencies'))
      ->setDescription(t('All currencies allowed for the store.'))
      ->setCardinality(-1)
      ->setRequired(TRUE)
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'commerce_currency')
      ->setSetting('handler', 'default')
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'options_buttons',
        'weight' => 20,
        'settings' => array(),
      ));

+21 −4
Changes for modules/store/src/Form/StoreForm.php: 21 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -24,10 +24,23 @@ class StoreForm extends ContentEntityForm {
    $form = parent::form($form, $form_state);
    $entity = $this->entity;

    $form['default_currency'] = array(
      '#type' => 'select',
      '#title' => t('Default currency'),
      '#options' => array('EUR' => 'EUR', 'GBP' => 'GBP', 'USD' => 'USD'),
    // Add default store config setting.
    $default_store = \Drupal::config('commerce_store.settings')->get('default_store');
    $is_default_store = FALSE;
    $disabled = FALSE;
    if (!empty($default_store) && $default_store == $entity->uuid()) {
      $is_default_store = TRUE;
    }
    if (empty($default_store)) {
      $is_default_store = TRUE;
      $disabled = TRUE;
    }
    $form['is_default_store'] = array(
      '#type' => 'checkbox',
      '#title' => t('Default store'),
      '#default_value' => $is_default_store,
      '#disabled' => $disabled,
      '#weight' => 0,
    );

    return $form;
@@ -39,6 +52,10 @@ class StoreForm extends ContentEntityForm {
  public function save(array $form, FormStateInterface $form_state) {
    try {
      $this->entity->save();
      // Save the default store config setting.
      if (!$form_state->isValueEmpty('is_default_store') && $form_state->getValue('is_default_store') != FALSE) {
        \Drupal::configFactory()->getEditable('commerce_store.settings')->set('default_store', $this->entity->uuid())->save();
      }
      drupal_set_message($this->t('Saved the %label store.', array(
        '%label' => $this->entity->label(),
      )));