You are here

public function CheeseburgerMenu::checkStore in Cheeseburger Menu 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/CheeseburgerMenu.php \Drupal\cheeseburger_menu\Plugin\Block\CheeseburgerMenu::checkStore()

Check if store has phone field.

File

src/Plugin/Block/CheeseburgerMenu.php, line 749
Cheeseburger class extends BlockBase.

Class

CheeseburgerMenu
Block info.

Namespace

Drupal\cheeseburger_menu\Plugin\Block

Code

public function checkStore(array &$form, FormStateInterface $form_state) {
  global $base_url;
  $store_id = $form_state
    ->getTriggeringElement()['#value'];
  if ($store_id != 0) {
    $store = Store::load($store_id);
    if ($store
      ->hasField('field_phone')) {
      try {
        $phone_number = current($store
          ->get('field_phone')
          ->getValue())['value'];
        if (empty($phone_number)) {
          $elem = [
            '#type' => 'textfield',
            '#value' => '<div class="description">This store doesn\'t have the phone number set up.<br>' . 'Edit it <a href="' . $base_url . '/store/' . $store_id . '/edit">here</a></div>',
            '#attributes' => [
              'id' => [
                'edit-phone-warning',
              ],
            ],
          ];
        }
        else {
          $elem = [
            '#type' => 'textfield',
            '#value' => 'Normal else',
            '#attributes' => [
              'id' => [
                'edit-phone-warning',
              ],
            ],
          ];
        }
      } catch (\Exception $exception) {
        $elem = [
          '#type' => 'textfield',
          '#value' => 'Catch',
          '#attributes' => [
            'id' => [
              'edit-phone-warning',
            ],
          ],
        ];
      }
    }
    else {
      $elem = [
        '#type' => 'textfield',
        '#value' => '<div class="description">You should add field with machine name field_phone to your store type</div>',
        '#attributes' => [
          'id' => [
            'edit-phone-warning',
          ],
        ],
      ];
    }
  }
  else {
    $elem = [
      '#type' => 'textfield',
      '#value' => 'manual',
      '#attributes' => [
        'id' => [
          'edit-phone-warning',
        ],
      ],
    ];
  }
  return $elem;
}