You are here

class Meta in Easymeta 8

Meta Class.

Hierarchy

  • class \Drupal\easymeta\Meta

Expanded class hierarchy of Meta

2 files declare their use of Meta
easymeta.module in ./easymeta.module
Contains easy_meta.module.
EasyMetaForm.php in src/Form/EasyMetaForm.php
1 string reference to 'Meta'
EasyMetaForm::buildForm in src/Form/EasyMetaForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().

File

src/Meta.php, line 8

Namespace

Drupal\easymeta
View source
class Meta {
  protected $id;
  protected $value;
  protected $url;
  protected $language;

  /**
   * Construct method.
   */
  public function __construct($language = NULL, $url = NULL) {
    if ($language && $url) {
      $metas = $this
        ->getMetasForUrlAndLanguage($url, $language);
      if ($metas) {
        $this->id = $metas->emid;
        $this->value = $metas->metas;
        $this->url = $url;
        $this->language = $language;
      }
    }
  }

  /**
   * Get Meta value.
   *
   * @return mixed
   *   Meta Value.
   */
  public function getValue() {
    return unserialize($this->value);
  }

  /**
   * Set Meta value.
   *
   * @param mixed $value
   *   The Meta value.
   */
  public function setValue($value) {
    $this->value = serialize($value);
  }

  /**
   * Get Meta Value.
   *
   * @return mixed
   *   The meta url.
   */
  public function getUrl() {
    return $this->url;
  }

  /**
   * Set Meta url.
   *
   * @param mixed $url
   *   The meta url.
   */
  public function setUrl($url) {
    $this->url = $url;
  }

  /**
   * Get Meta Language.
   *
   * @return mixed
   *   Meta language.
   */
  public function getLanguage() {
    return $this->language;
  }

  /**
   * Set Meta language.
   *
   * @param mixed $language
   *   Meta Language.
   */
  public function setLanguage($language) {
    $this->language = $language;
  }

  /**
   * Get Meta Id.
   *
   * @return mixed
   *   Meta Id.
   */
  public function getId() {
    return $this->id;
  }

  /**
   * Set Meta Id.
   *
   * @param mixed $id
   *   Meta Id.
   */
  public function setId($id) {
    $this->id = $id;
  }

  /**
   * Save and persist Meta.
   */
  public function save() {
    $array = [
      "url" => $this
        ->getUrl(),
      "language" => $this
        ->getLanguage(),
      "metas" => serialize($this
        ->getValue()),
    ];
    if ($this->id) {
      $query = \Drupal::database()
        ->update('easymeta');
      $query
        ->fields($array);
      $query
        ->condition('emid', $this->id);
      return $query
        ->execute();
    }
    else {
      $query = \Drupal::database()
        ->insert('easymeta');
      $query
        ->fields($array);
      return $query
        ->execute();
    }
  }

  /**
   * Retrieve Meta from $url and $language.
   */
  public function getMetasForUrlAndLanguage($url, $language) {
    $query = \Drupal::database()
      ->select('easymeta', 'em');
    $query
      ->fields('em');
    $query
      ->condition('url', $url);
    $query
      ->condition('language', $language);
    return $query
      ->execute()
      ->fetch();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Meta::$id protected property
Meta::$language protected property
Meta::$url protected property
Meta::$value protected property
Meta::getId public function Get Meta Id.
Meta::getLanguage public function Get Meta Language.
Meta::getMetasForUrlAndLanguage public function Retrieve Meta from $url and $language.
Meta::getUrl public function Get Meta Value.
Meta::getValue public function Get Meta value.
Meta::save public function Save and persist Meta.
Meta::setId public function Set Meta Id.
Meta::setLanguage public function Set Meta language.
Meta::setUrl public function Set Meta url.
Meta::setValue public function Set Meta value.
Meta::__construct public function Construct method.